引用另一个类时的 iStream 重载

istream overloading when referencing another class

本文关键字:iStream 重载 另一个 引用      更新时间:2023-10-16

我一直在 istream 运算符中如何处理这个 for 循环,我不明白为什么它找不到>>运算符。

错误 C2679 二进制">>":未找到采用类型为"Result"的右侧操作数的运算符(或者没有可接受的转换)

任何帮助将不胜感激。


#include "pch.h"
#include <iostream>
#include <fstream>
#include "Unit.h"
#include "Registration.h"
istream & operator >>(istream & input, Unit & U);
ostream & operator <<(ostream & os, const Unit & U);
istream & operator >>(istream & input, Result & Re);
ostream & operator <<(ostream & os, const Result & Re);
istream & operator >>(istream & input, Date & D);
ostream & operator <<(ostream & os, const Date & D);
int main()
{
std::cout << "Hello World!n"; 
ifstream infile("rinput.txt");
if (!infile)
{
return -1;
}
Registration R;
infile >> R;
ofstream ofile("routput.txt");
ofile << R
<< "Number of units = " << R.getCount() << 'n'
<< "Total credits     = " << R.GetCredits() << 'n';
infile.close();
ofile.close();
}

#include "Registration.h"
using namespace std;
Registration::Registration()
{
}
istream & operator >>(istream & input, Registration & R)
{
long idTemp;
unsigned semesterTemp, countTemp;
Registration regTemp;
Result tempResult;
input >> idTemp >> semesterTemp >> countTemp;
regTemp.setStudentId(idTemp);
regTemp.setSemester(semesterTemp);
regTemp.setCount(countTemp);
for (unsigned i = 0; i < R.getCount(); i++)
{       
//THIS LINE IS THE ERROR
input >> tempResult;
R.setResult(i, tempResult);
}       
return input;
}

//result h file has the following
#ifndef RESULT_H
#define RESULT_H
#include "Date.h"
#include "Unit.h"
class Result
{
public:
Result();
float getMark() const;
void setMark(float mrk);
int GetCredits() const;
const Date & getDate() const;
void setDate(const Date &D);
const Unit & getUnit() const;
void setUnit(const Unit &U);
private:
float mark;
Date dateObject;
Unit unitObject;
};

#endif // !RESULT_H

这是错误:

Severity    Code    Description Project File    Line    Suppression State
Error   C2679   binary '>>': no operator found which takes a right-hand operand of type 'Result' (or there is no acceptable conversion) 

在 Result 中引用的每个类也有一个重载输入流函数。

这是一个简单的例子

namespace Me {
class A 
{
// bla
};
std::ostream& operator<<(std::ostream& os, A& const& obj)
{
// print in terms of public interface of A
// (else, deckare this a friend function inside A)
return os;
} 
} // Me
int main()
{
std::cout << A(); // operator<<(ostream&, A const&) is the best match
}

C++并没有真正的"源"或"头"文件,它只处理翻译单元

简而言之,翻译单元是包含所有头文件的单个源文件。最重要的是,翻译单元是它自己的独立单元,不了解其他翻译单元。

这意味着放在一个翻译单元(源文件)中的声明现在将由另一个翻译单元知道。

为了使链接到最终可执行程序的所有翻译单元都知道声明,应将它们放在包含在需要的任何位置的头文件中


例:

头文件result.h

#ifndef RESULT_H
#define RESULT_H
class Result
{
// ...
};
std::istream & operator >>(std::istream & input, Result & C);
std::ostream & operator <<(std::ostream & os, const Result & C);
#endif

"主"源文件main.cpp

#include <iostream>
#include "result.h"    // "Import" the `operator>>` declaration
int main()
{
Result result;
std::cin >> result;
}

"结果"类实现源文件result.cpp

#include <iostream>
#include "result.h"
// Result class implementation...
std::istream & operator >>(std::istream & input, Result & C)
{
// Read input...
return input;
}

生成上述示例时,将两个源文件分别构建为两个单独的翻译单元。每个翻译单元的结果都是一个对象文件。然后,将这两个目标文件链接到可执行文件中。