2015-04-26 142 views
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 

我会很乐意的任何援助。谢谢。

+0

可以file1和file2打开同一个文件吗? – antonpp

回答

2

传递给你的提取操作的参数是不正确的:

template<typename T> 
istream& operator>>(istream &in, const config<T> &w) 

你不能很好地提取从流值放入w,如果它是const!更改为config<T> - const参考。

+0

非常感谢,它的工作! – raz