0
我需要重載operator >>
來幫助我從文件讀取配置。這就是我想出了:C++操作符「>>」重載錯誤
template<typename T>istream& operator>>(istream &in, const config<T> &w)
{
in>>w.attribName1;
for(int z=0;z<3;z++)
{
in>>w.attribute1[z];
}
in>>w.attribName2;
for(int x=0;x<3;x++)
{
in>>w.attribute2[x];
}
in>>w.attribName3;
for(int v=0;v<3;v++)
{
in>>w.attribute3[v];
}
in>>w.attribName4;
for(int n=0;n<3;n++)
{
in>>w.attribute4[n];
}
return in;
}
這是用這樣的:
int _tmain(int argc, _TCHAR* argv[])
{
double val2[3]={4.124,7.07,12.01};
string FILE1NAME,FILE2NAME,file1name1,file1name2,file1name3,file1name4;
FILE1NAME="Config_file_no1";
FILE2NAME="cfg2";
file1name1="Volume";
file1name2="Screen_mode";
file1name3="Color_settings";
file1name4="Sensitivity";
ofstream file1(FILE1NAME+".ini");
ifstream file2(FILE1NAME+".ini");
config<double> first;
config<double> second;
first.setAttribName1(file1name1);
first.setAtt1(val2);
first.setAttribName2(file1name2);
first.setAtt2(val2);
first.setAttribName3(file1name3);
first.setAtt3(val2);
first.setAttribName4(file1name4);
first.setAtt4(val2);
file2 >> second;
system("pause");
return 0;
}
我得到同樣的事情多個錯誤:
Error 1 error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion) d:\(path) 193 1 projekt_v2
我會很樂意的任何援助。謝謝。
可以file1和file2打開同一個文件嗎? – antonpp