C ++,得到一个"没有运算符">>"匹配这些操作数"。对于读取文件的函数
c ++ , getting a 'no operator ">>" matches these operands'. For a function that reads a file
我在这个代码中有错误:在"in>>point"中">>"的while循环中
void FileRead(const int cap,const string point,int counter)
{
ifstream in;
in.open("sample_strings.txt"); //ifstream in; in.open("sample_data.txt");
if (in.fail())
cout<<"sample_data not opened correctly"<<endl;
while(!in.eof() && counter<cap)
{
in>>point[counter];
counter++;
}
in.close();
}
我尝试实现我需要>>运算符,但仍然收到错误。
ifstream & operator>>(ifstream & in, ARRAY & Original)
{
cout<<"operator>> has been calledn";
Original.counter = 0;
while(!in.eof() && Original.count<Original.cap)
{
in>>Original. point[Original.counter];
(Original.counter)++;
}
return in;
}
我正在实现读取文件函数以在传递构造函数时读取构造函数的数组
我拥有的私人功能是这些
private:
string *point;
int counter;
int cap;
我仍然是 c++ 的新手,所以任何理解这一点的小帮助都会有所帮助。
您正在尝试读入const string
。不是传递const string point
,而是传递string& point
作为对字符串的可修改引用传递。
线路in>>point[counter]
在point
上调用operator[]
,然后调用operator>>
。但是因为point
是一个const string
,operator[]
返回一个不应该读入的const char&
。你希望它返回char&
,所以point
必须是非常量。
相关文章:
- 控制允许动态运行c++的并发操作数
- 为什么C++逐位AND运算符在不同大小的操作数中表现为这样
- EASTL矢量<向量<int>>连续的
- 错误 C2679:二进制"<<":未找到采用类型 'std::string_view' 的右侧操作数的运算符(或者没有可接受的转换)
- C++ 带有向量的 IF 计算操作数
- 是否所有C++运算符都接受操作数?
- 一元*运算符的操作数是否期望一个 prvalue
- 与'operator='不匹配(操作数类型'String'且"void")
- 不正确的操作数类型 MSVC
- C2678 二进制 '==':未找到采用 'Card' 类型左操作数的运算符(或者没有可接受的转换)
- 错误:类型"double()"和"double()"的操作数无效到二进制&quo
- 我的运算符重载是否有效<<(流插入)左操作数不是 ostream
- 没有运算符"="与这些操作数匹配,JUCE 窗口演示应用程序
- 如果我在指针中使用 ++ 操作数,我的值就会出错
- 二进制表达式的操作数无效 - 使用 for 和迭代器
- C2679 二进制"<<":未找到采用右操作数类型的运算符
- CPP 运算符重载操作数位置 [] 与 ==
- int* 和 int[] 类型对二进制运算符 + 的操作数无效
- 错误:二进制'operator*' 'float'和'float[0]'类型的操作数无效
- 运算符'<'不能应用于类型 'string' 和 'string' 的操作数