c++ - 不能在 C++ 中重载运算符>>

标签 c++ overloading operator-keyword

我正在学习 C++,但当我尝试将 Java 代码转换为 C++ 时,我一直遇到此错误。我在 java 中有一些类,我必须将它们翻译成 C++ 代码(使用 C++ 样式编程)。我有一个 Article 类、继承自 Article 的 ArticleUnitaire 类和继承自 ArticleUnitaire 的 Ramette 类。所有的 getter 和 setter 都工作得很好,问题是当我尝试执行相当于 c++ 的 toString 时,这是 operator<< 重载。让我向您展示代码和错误: Java代码:

    public abstract class Article {
      ....
      public String toString() {
        return this.getClass().getName() + ":reference=" 
          + reference + ";descriptif=" + getDescriptif() 
          + ";marque=" + getMarque() + ";PU=" + getPU();
      }

    }

public abstract class ArticleUnitaire extends Article {
  private String marque;
  private double pu;
  private String descriptif;

  public ArticleUnitaire(String reference) {
    super(reference);
  }

  public ArticleUnitaire(String reference, String descriptif, String marque,
                         double pu) {
    super(reference);
    this.marque = marque;
    this.pu = pu;
    this.descriptif = descriptif;
  }



  // useless to redefine toString because PU and descriptif
  // were also displayed by the superclass(Article)

}

public class Stylo extends ArticleUnitaire {
 ....

  @Override
  public String toString() {
    return super.toString() + ";couleur=" + couleur;
  }
}

这是我的 articles.h 文件:

    #include <string>
    using namespace std;



    class Article
    {
        public:
        string getReference();
        virtual string getDescriptif() const;
        virtual double getPU() const;
        string getMarque() const;
        void Afficher(ostream&) const;
        ostream& operator<<(ostream&) const;

        protected:
        Article(string&);
            string reference;
        private:

    };

    class ArticleUnitaire : public Article {
        public:
          ArticleUnitaire(string);
          ArticleUnitaire(string, string, string, double);
          string getMarque() const;
          void setMarque(string&);
          double getPU() const;
          void setPU(double&);
          void setDescriptif(string&);
          string getDescriptif() const;
          void Afficher(ostream&) const;

        protected:
          string marque;
          double pu;
          string descriptif;
        private:

    };

    class Stylo : public ArticleUnitaire {
        public:
          Stylo(string r, string d, string m, double p, string c);
          void Afficher(ostream& os) const;
          string getCouleur();
        protected:
        private:
          string couleur;

    };

class Lot : public Article {
    public:
      Lot(string, Article, int, int);
      double getPU() const;
      string getDescriptif() const;
      string getMarque() const;
      int getNbArticles() const;
      void setNbArticles(int&);
      int getPourcentage() const;
      void setPourcentage(int&);
      Article getArticle() const;
      void setArticle(Article&);
      virtual void Afficher(ostream& os) const;


    protected:
    private:
      int nb;
      int pourcentage;
      Article art;
};

和articles.cc文件:

    #include <typeinfo>
    #include <string>
    using namespace std;
    #include "articles.h"

    /*   Article   */

    Article::Article(string& ref) : reference(ref) {};

    string Article::getReference() {
      return reference;
    }

    ostream& Article::operator<<(ostream& os) const {
      Afficher(os);
      return os;
    }

    string Article::getMarque() const {
      return "Sans marque";
    }

    void Article::Afficher(ostream& os) const {
      os << " : reference = " << getReference() << " ; descriptif = " << getDescriptif() << " ; marque = " << getMarque() << " ; PU = " << getPU();
    }

    /*   Article Unitaire   */

    ArticleUnitaire::ArticleUnitaire(string r) : Article(r) {};

    ArticleUnitaire::ArticleUnitaire(string r, string d, string m, double p) : Article(r), marque(m), descriptif(d), pu(p) {};

    string ArticleUnitaire::getMarque() const {
      return marque;
    }

    void ArticleUnitaire::setMarque(string& m) {
      marque = m;
    }

    double ArticleUnitaire::getPU() const {
      return pu;
    }

    void ArticleUnitaire::setPU(double& p) {
      pu = p;
    }

    void ArticleUnitaire::setDescriptif(string& d) {
      descriptif = d;
    }

    string ArticleUnitaire::getDescriptif() const {
      return descriptif;
    }

    /*   Stylo    */

    Stylo::Stylo(string r, string d, string m, double p, string c) : ArticleUnitaire(r,d,m,p), couleur(c) {};

    string Stylo::getCouleur() {
      return couleur;
    }

    void Stylo::Afficher(ostream& os) const {
      Article::Afficher(os);
      os << " ; couleur = " << getCouleur();
    }

Lot::Lot(String r, Article a, int n, int p) : Article(r) {
  art = a;
  nb = n;
  pourcentage = p;
};
...

但是,当我尝试编译时,方法 Afficher() 始终出现相同的错误:

In member function ‘void Article::Afficher(std::ostream&) const’:
articles.cc:24:9: error: no match for ‘operator<<’ in ‘os << " : reference = "’
articles.cc:24:9: note: candidate is:
In file included from /usr/include/c++/4.7/string:54:0,
                 from articles.cc:2:
/usr/include/c++/4.7/bits/basic_string.h:2750:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.7/bits/basic_string.h:2750:5: note:   template argument deduction/substitution failed:
articles.cc:24:9: note:   mismatched types ‘const std::basic_string<_CharT, _Traits, _Alloc>’ and ‘const char [16]’
articles.cc:24:43: error: passing ‘const Article’ as ‘this’ argument of ‘std::string Article::getReference()’ discards qualifiers [-fpermissive]
articles.cc: In member function ‘void Stylo::Afficher(std::ostream&) const’:
articles.cc:67:9: error: no match for ‘operator<<’ in ‘os << " ; couleur = "’
articles.cc:67:9: note: candidate is:
In file included from /usr/include/c++/4.7/string:54:0,
                 from articles.cc:2:
/usr/include/c++/4.7/bits/basic_string.h:2750:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.7/bits/basic_string.h:2750:5: note:   template argument deduction/substitution failed:
articles.cc:67:9: note:   mismatched types ‘const std::basic_string<_CharT, _Traits, _Alloc>’ and ‘const char [14]’
articles.cc:67:39: error: passing ‘const Stylo’ as ‘this’ argument of ‘std::string Stylo::getCouleur()’ discards qualifiers [-fpermissive]
articles.cc: In member function ‘void Ramette::Afficher(std::ostream&) const’:
articles.cc:79:9: error: no match for ‘operator<<’ in ‘os << " ; grammage = "’
articles.cc:79:9: note: candidate is:
In file included from /usr/include/c++/4.7/string:54:0,
                 from articles.cc:2:
/usr/include/c++/4.7/bits/basic_string.h:2750:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.7/bits/basic_string.h:2750:5: note:   template argument deduction/substitution failed:
articles.cc:79:9: note:   mismatched types ‘const std::basic_string<_CharT, _Traits, _Alloc>’ and ‘const char [15]’
articles.cc:79:41: error: passing ‘const Ramette’ as ‘this’ argument of ‘int Ramette::getGrammage()’ discards qualifiers [-fpermissive]
articles.cc: At global scope:
articles.cc:84:9: error: expected constructor, destructor, or type conversion before ‘(’ token
articles.cc: In member function ‘virtual std::string Lot::getDescriptif() const’:
articles.cc:91:26: error: invalid operands of types ‘const char*’ and ‘const char [3]’ to binary ‘operator+’
articles.cc: In member function ‘void Lot::Afficher(std::ostream&) const’:
articles.cc:124:9: error: no match for ‘operator<<’ in ‘os << " ;reduction = "’
articles.cc:124:9: note: candidate is:
In file included from /usr/include/c++/4.7/string:54:0,
                 from articles.cc:2:
/usr/include/c++/4.7/bits/basic_string.h:2750:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.7/bits/basic_string.h:2750:5: note:   template argument deduction/substitution failed:
articles.cc:124:9: note:   mismatched types ‘const std::basic_string<_CharT, _Traits, _Alloc>’ and ‘const char [15]’

如您所见,编译器不喜欢我在运算符>> 重载中使用的方法 Afficher 所做的事情。我做错了什么?

最佳答案

一些问题:

  • Stylo::getCouleur() 不是常量

  • Ramette::getGrammage() 不是常量

这是我最先发现的两个

关于c++ - 不能在 C++ 中重载运算符>>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13980475/

相关文章:

C++ << 没有友元函数的运算符重载

c++ - 是否可以在结构之外编写自动转换运算符?

c++ - Juno CDT 插件无法运行 C++ 应用程序

c++ - 迭代 std::string 元素

c++ - 如何在 C++ 中对类使用运算符重载函数?

ruby - Ruby 中的方法覆盖

作为函数参数的 C++ bool 表达式调用错误的重载

C++:自定义数据类型 - 类型转换和 union 问题

c++ - Linux 的 COleDateTime 替代品

c++ - catch-all 处理程序中的 exception_ptr 在 Centos 7 和 Windows 上运行异常