2012-12-20 63 views
0

我在C++的學習過程中,但當我嘗試做從java代碼到C++的翻譯時,我一直有這個錯誤。我有一些java的類,我必須翻譯成C++代碼(使用C++風格的編程)。我有一篇文章類,繼承自Article的ArticleUnitaire類,以及繼承自ArticleUnitaire的類Ramette。所有的getter和setter工作得很好,問題是當我嘗試執行與C++相當的toString時,這是運算符< <重載。讓我告訴你的代碼和錯誤: Java代碼:不能重載運算符>>在C++

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。我究竟做錯了什麼?

+1

我沒有嘗試通讀代碼,但首先想到的是,你是否包含了?那麼爲什麼'getReference'不是const? –

+0

和你的「getReference」應該是const。 –

+0

是的,你對getReference絕對正確,我錯過了,因爲其他類似的函數是const的,對不起我的壞。但是,我試圖包含iostream,並且錯誤數量增加了三倍。顯然,如果我不包括它,我只有上面的錯誤,但如果我包含它,我有4倍以上的錯誤。我不明白。我非常仔細地嘗試。我不知道我錯在哪裏。 –

回答

1

一些問題:

  • 柱花草:: getCouleur()不是const的

  • 拉米特(Ramette):: getGrammage()不是const的

這是兩個我找到的第一批

0

過載operator<<作爲非成員函數,而不是我mber功能。這是實現它的唯一方法,因爲您希望您的對象位於操作員的右側,而不是左側(並且您無法更改類std::ostream)。

編輯:但是,非成員函數可以依次調用虛成員函數...

頭文件:

class Article { 
public: 
    virtual void Afficher(std::ostream& os) const; 
    //... 
}; 
std::ostream& operator<<(std::ostream& os, const Article& art); 

class ArticleUnitaire : public Article { 
public: 
    virtual void Afficher(std::ostream& os) const; 
    //... 
}; 

inline std::ostream& operator<<(std::ostream& os, const Article& art) { 
    art.Afficher(os); 
    return os; 
} 

源文件:

void Article::Afficher(std::ostream& os) const { 
    //... 
} 

void ArticleUnitaire::Afficher(std::ostream& os) const { 
    Article::Afficher(os); 
    //Other data... 
} 
+0

是的,但我有義務使用它作爲成員函數,因爲我需要在子類中重新定義它。例如在Stylo中,我從Article類調用Afficher方法(如java中的super),然後將其與子類中的特定類型連接起來。例如,在Stylo類中,我顯示Article類的Afficher方法,並且我還想添加couler屬性。它就像「超級連接」couleur = getcouleur()「」 –

+0

編輯我的答案更多細節。 – aschepler

+0

我不明白:爲什麼你把Afficher作爲虛擬的,因爲你在源代碼中定義它? –