关于C++重载运算符的问题
#include<string>#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
class A{
public:
A(int a=0,string tmp="") :m_c(a), m_str(tmp){};
friend ostream & operator <<(ostream& out, const A& tmp)
{
return out << tmp.m_c<< tmp.m_str;
}
friend istream & operator >>(istream& t, A& tmp)
{
return t >> tmp.m_str >> tmp.m_c;
}
private:
int m_c;
string m_str;
int m_b;//////////////////////////////////////////////////////////////////////////////////问题所在///////////////////////////////////
};
void func(vector<A>& tmp)
{
ofstream ofs;
ofs.open("./a.txt", ios::out);
vector<A>::iterator it = tmp.begin();
while (it!=tmp.end())
{
ofs << *it << endl;
it++;
}
ofs.close();
}
void func2(vector <A>& tmp)
{
ifstream ifs;
ifs.open("./a.txt", ios::in);
A a;
while (!ifs.eof())
{
ifs >> a;
tmp.push_back(a);
}
ifs.close();
}
int main(void)
{
vector<A> ver;
vector<A> vec;
A a(1, "xuwe12i");
A b(2, "xweifds");
A c(3, "xweiwe");
vec.push_back(a);
vec.push_back(b);
vec.push_back(c);
func(vec);
func2(ver);
vector<A>::iterator it = ver.begin();
while (it != ver.end())
{
cout << *it << endl;
++it;
}
system("pause");
return 0;
}
问题:为什么那个m_b 如何我不注释掉 就没办法从文件读出东西,只有注释了 就可以正常运行 我都可以{:9_239:} DarkSE 发表于 2016-9-7 13:37
我都可以
我加上那个m_d之后 就是一个死循环,打印不出来,你把代码贴出来看看 陌雨 发表于 2016-9-7 15:45
我加上那个m_d之后 就是一个死循环,打印不出来,你把代码贴出来看看
我直接复制你的代码注释掉而已……
#include<string>
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
class A{
public:
A(int a=0,string tmp="") :m_c(a), m_str(tmp){};
friend ostream & operator <<(ostream& out, const A& tmp)
{
return out << tmp.m_c<< tmp.m_str;
}
friend istream & operator >>(istream& t, A& tmp)
{
return t >> tmp.m_str >> tmp.m_c;
}
private:
int m_c;
string m_str;
//int m_b;//////////////////////////////////////////////////////////////////////////////////问题所在///////////////////////////////////
};
void func(vector<A>& tmp)
{
ofstream ofs;
ofs.open("./a.txt", ios::out);
vector<A>::iterator it = tmp.begin();
while (it!=tmp.end())
{
ofs << *it << endl;
it++;
}
ofs.close();
}
void func2(vector <A>& tmp)
{
ifstream ifs;
ifs.open("./a.txt", ios::in);
A a;
while (!ifs.eof())
{
ifs >> a;
tmp.push_back(a);
}
ifs.close();
}
int main(void)
{
vector<A> ver;
vector<A> vec;
A a(1, "xuwe12i");
A b(2, "xweifds");
A c(3, "xweiwe");
vec.push_back(a);
vec.push_back(b);
vec.push_back(c);
func(vec);
func2(ver);
vector<A>::iterator it = ver.begin();
while (it != ver.end())
{
cout << *it << endl;
++it;
}
system("pause");
return 0;
} 我试过了,拿你的代码原样就可以运行啊也没死循环
页:
[1]