引用另一个类时的 iStream 重载
istream overloading when referencing another class
我一直在 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;
}
生成上述示例时,将两个源文件分别构建为两个单独的翻译单元。每个翻译单元的结果都是一个对象文件。然后,将这两个目标文件链接到可执行文件中。
相关文章:
- 逗号在重载 ostream 和 istream 在结构中分开
- Istream 运算符重载'>>'导致无限循环
- 尝试重载时'ostream'和'istream'错误C++
- 使用 getline 重载 istream 运算符>> 会出错
- 错误:"运算符>>"不匹配 重载 istream 运算符
- 引用另一个类时的 iStream 重载
- 重载运算符'>>'的使用不明确(操作数类型'istream'(又名"basic_istream<char>")和"MyIncreEx")
- 为什么我可以重载 istream 的运算符>>用于字符串?
- 为什么我们需要在重载>>和<<运算符时返回对 istream/ostream 的引用?
- 对C++中的istream和ostream运算符进行双重重载
- 使用 istream 重载>>
- istream 运算符重载>>将不起作用
- 运算符重载:Ostream/Istream
- 在C++中使用非友元方法重载 I/O 运算符 |&istream 和 &ostream 函数
- iStream 重载 - 从文件中读取字符串
- 重载 istream >>而不返回
- C++istream运算符重载-即使声明为友元,也无法访问数据成员
- 运算符重载 c++ >> istream
- 为什么 istream::operator>> 没有占用字符的重载?
- 仅在重载>>运算符 istream 中获取字符串: