2015-04-01 20 views
0

在此先感谢您的帮助!该计划是一项学生注册计划。C++ ostream:没有运算符匹配<<&期望的初始值设定符&'token'

输入是通过文件,结果输出到文件。然后一般的想法是学习C++,方法重载 - 包括运算符和fstream的。我会发布编译器指向的代码,但请让我知道如果你想看到其他代码。

当我去编译错误点到我的头文件方法,在我的课外,但在#endif内。见下文。

std::ostream & operator << (std::ostream & os, const Registration & R); 

这个文件的实现是这样的:

std::ostream &operator <<(std::ostream & os, const Registration & R ) 
{ os << "Student ID: " << R.GetStudentId() << "\n" 
     << "Semester: " << R.GetSemester() << "\n \n"; 

for(unsigned i = 0; i < R.GetCount(); i++) 
{ 
    os << "\tUnit Name : " << R.GetName(i) << "\n" 
     << "\tUnit Id : " << R.GetId(i) << "\n" 
     << "\tCredit Points : " << R.GetCredits(i) << "\n" 
     << "\tStudent Mark : " << R.GetResult(i) << "\n" 
     << "\tDate : " << R.GetDay(i) << " " << R.GetMonth(i) << " " << R.GetYear(i) << "\n\n"; 
} 

os << "Total Credits :" << R.GetCredits() << "\n" 
    << "Number of Units : " << R.GetCount() << "\n" 
    << "Average Mark : " << R.GetAverage(); 

return os; 
} 

而且main()中再次

int main() 
{ 
ifstream infile("rinput.txt"); 
if(!infile) return -1; 

Registration R; 
infile >> R; 

ofstream ofile("routput.txt"); 

ofile << R; // Error Here RE- no match for 'operator<<' 

cout << "\nComputation completed. Check output file. \n"; 

return 0; 
} 

感谢您的任何和所有帮助,希望这不是一个新秀错误...我在哪里迟到了。

编辑:我在所有的头文件中都使用了“using namespace std”#& #include。

+1

你忘了'#include '。 – 2015-04-01 16:59:06

+0

@Kerrek - 在我所有的头文件中都有:) – 2015-04-01 17:04:55

+0

@MasterSketchiggle请发布[MCVE](http://stackoverflow.com/help/mcve),让每个人都能重现你的错误。 – 2015-04-01 17:14:58

回答

0

此代码是有效的,除了:

  • 极品#include <iostream>
  • 需要或者添加using namespace std;或添加std::ifstreamofstream,并cout之前。
  • 需要重载istream为您的Registration类。

编辑:请添加std::而不是using namespace std;。我只是提到它的完整性。 :)

+2

_“需要添加'使用命名空间标准;'”_这会导致我身体上的痛苦 – 2015-04-01 17:10:00

+0

我同意,并将在上面编辑 – 2015-04-01 17:10:24

+0

在uni atm。教导使用“使用命名空间标准” 我可以问为什么看起来你建议我总是在编码时把std :: 另外,我有iostream在所有头文件中并且在所有头文件中使用“命名空间标准” I已经重载了注册类的istream,但没有包含代码,因为错误似乎并没有涉及到这一点,我应该更新这个问题吗? – 2015-04-01 17:15:46

相关问题