2012-12-20 2 views
0

저는 C++의 학습 과정에 있지만 자바 코드에서 C++ 로의 변환을 시도 할 때이 오류가 계속 발생합니다. C++ 코드 (C++ 스타일 프로그래밍 사용)로 변환해야하는 java 클래스가 있습니다. 나는 Article 클래스, Article로부터 상속받은 ArticleUnitaire 클래스, ArticleUnitaire로부터 상속받은 Ramette 클래스를 가진다. 모든 getters와 setter는 잘 작동하지만, 문제는 toString을 C++과 동등하게하려고 할 때입니다. 연산자는 < <입니다. 자바 코드 : 내가 당신에게 코드와 오류를 보여 드리죠 여기 은 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을 포함 시키려고 시도했지만 오류 수가 3 배 증가했습니다. 분명히, 내가 그것을 포함하지 않으면, 나는 단지 위의 오류가 있지만 내가 그것을 포함하면 나는 4 배 더 많은 오류가있다. 난 이해가 안 돼요. 나는 그것을 아주 조심스럽게 시도했다. 나는 내가 어디 잘못되었는지 모른다. –

답변

1

일부 문제 :

  • STYLO :: getCouleur()

  • Ramette :: getGrammage을 (에서 const되지 않음)이 두입니다

을에서 const되지 않는다 내가 처음 발견 한 것들

0

과부하 operator<<은 비회원 기능으로서, 저로서는 그렇지 않습니다. 광 기능. 이 방법은 왼쪽에서가 아니라 연산자의 오른쪽에 객체를 놓을 수있는 유일한 방법입니다 (클래스 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

나는 이해하지 못한다 : 당신이 소스에서 그것을 정의한 이후로 왜 가상을 가상으로 놓는가? –

관련 문제