【问题标题】:Trying to make a write function for a class and getting error试图为一个类创建一个写函数并得到错误
【发布时间】:2012-03-18 13:00:09
【问题描述】:

我已经为我的班级实现了一个写入功能,但我收到了错误:

class Fa{
private:
  string Q_;
  string F_;

public:
   void write(ostream& out = cout) const{
   out<<Q_<<endl;
   out<<F_<<endl;
   }
};

错误:为 `void Fa::write(std::ostream&) const' 的参数 1 提供默认参数

谁能告诉我这个错误是什么意思,我该如何避免它?

【问题讨论】:

    标签: c++


    【解决方案1】:

    您缺少包含和命名空间:

    #include <iostream>
    #include <string>
    
    class Fa{
      private:
        std::string Q_;
        std::string F_;
    
      public:
        void write(std::ostream& out = std::cout) const{
          out << Q_<< std::endl;
          out << F_<< std::endl;
        }
    };
    

    如果你有一个头文件和一个cpp文件,在头文件中指定默认参数:

      /* ... */
      public:
        void write(std::ostream& out = std::cout) const;
      /* ... */
    

    并且在 cpp 文件中不要:

        void Fa::write(std::ostream& out) const {
           /* ... */
        }
    

    【讨论】:

    • 即使我使用 std::string 添加了;使用 std::ostream;使用 std::cout;使用 std::endl;我以前没有,我仍然得到同样的错误。
    • 你在谈论哪个论点
    • 正如我所说的那样,我得到了这个错误:[链接器错误]未定义对`operator
    • 这并不真正相关。您正在使用 &lt;&lt; 运算符作为转换类型而没有重载。你需要像 std::ostream &amp;operator &lt;&lt; (std::ostream &amp;out, const Fa::Transition const&amp;) { /* ... */ } 这样的 smt 见:lifecs.likai.org/2010/10/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-19
    • 2013-01-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多