【问题标题】:Error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::ifstream'错误 C2678:二进制“>>”:未找到采用“std::ifstream”类型的左侧操作数的运算符
【发布时间】:2020-02-03 08:17:30
【问题描述】:

我正在开发一个 C++ Windows 窗体应用程序,当用户单击按钮时,它会从 txt 文件中读取点击计数,然后执行一些功能并将新的点击计数写回 txt 文件。

当只有ofstream代码时,它运行良好。但是在我添加了ifstream代码之后,出现了这个错误:

错误 C2678:二进制“>>”:未找到采用“std::ifstream”类型的左侧操作数的运算符(或没有可接受的转换)

我已经包含了<fstream> 和<iostream>。

private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) 
{
    //Some visible and invisible code for label

    std::ifstream file1;
    file1.open("Progress.txt");
    file1 >> clickcount;
    file1.close();

    clickcount = ++clickcount;

    ...

    std::ofstream file;
    file.open("Progress.txt");
    file << clickcount;
    file.close();

【问题讨论】:

  • 对不起。我已经编辑过了
  • clickcount 声明为什么?
  • public ref class NNYT : public System::Windows::Forms::Form { unsigned short int clickcount = 0 ...

标签: c++ winforms


【解决方案1】:

我的问题已经解决了

像这样声明点击计数:

#pragma endregion
   private: System::Void button1_Click_1(System::Object^  sender, System::EventArgs^  e) {
       int clickcount;
       ...}

不在public ref class

【讨论】:

  • 将其声明为 Form 类的成员并没有错。使其成为单击事件处理程序的局部变量将无法再跟踪多次单击的计数。更不用说一个事件处理程序无法访问另一个事件处理程序的局部变量(您的“解决方案”使其成为 button1 的本地变量,但您的问题是关于在 button2 中使用它)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-18
相关资源
最近更新 更多