【问题标题】:C++ Netbeans cout errorC++ Netbeans cout 错误
【发布时间】:2010-07-30 08:34:30
【问题描述】:

我在开发 C++ 中从未遇到过这些错误。当我在 Netbeans 中使用 cout 时,它让我无法匹配操作员错误。 这是我的程序



#include <cstdlib> #include <fstream> #include &lt;iostream&gt; using namespace std;

int main() { //read file fstream fileStream; string tempStr; string strText; fileStream.open("fincode.htm", ios::in); while(!fileStream.eof()) { fileStream >> tempStr; strText += tempStr; cout >> strText; }

fileStream.close();
return 0;

}

我在构建时遇到以下错误


main.cpp: In function ‘int main()’:
make[1]: Leaving directory `/home/user/NetBeansProjects/read_page2'
main.cpp:31: error: no match for ‘operator>>’ in ‘std::cout >> strText’
make[2]: * [build/Debug/GNU-Linux-x86/main.o] Error 1
make[1]: * [.build-conf] Error 2
make: *** [.build-impl] Error 2

如果我删除 cout,我不会收到任何错误。知道发生了什么吗?

【问题讨论】:

  • 对不起!这对我来说是一个愚蠢的错误。当我将 >> 更改为

标签: netbeans


【解决方案1】:

你需要:

#include <string>

另外,你的 while 循环是错误的——你通常不应该使用 eof() 函数作为循环控制——使用:

  while( fileStream >> tempStr )
   {
     strText += tempStr;
     cout << strText;      // note operator <<
   }

我刚刚注意到你的 cout 语句也不正确。

【讨论】:

  • @Graham 考虑一下如果文件为空会发生什么。 eof() 仅在您尝试阅读某些内容后才有效。
  • 谢谢。我为 cout 使用了错误的运算符。现在可以了。 eof() 的优点。我也对 while 语句进行了更改。
【解决方案2】:

添加

#include <string>

然后改变

cout >> strText;

cout << strText; 

【讨论】:

  • 不需要#include
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多