저는 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 메서드로 수행중인 작업을 좋아하지 않습니다. 내가 도대체 뭘 잘못하고있는 겁니까?
내가하지 않은 코드를 통해 읽으려고하지만, 처음 생각하면을 포함 시켰습니까? 그렇다면'getReference'가 const가 아닌 이유는 무엇입니까? –
그리고 "getReference"는 const 여야합니다. –
예, 당신은 getReference에 대해 절대적으로 옳았습니다. 다른 비슷한 기능들이 const이기 때문에 나는 그것을 놓쳤습니다. 그러나 iostream을 포함 시키려고 시도했지만 오류 수가 3 배 증가했습니다. 분명히, 내가 그것을 포함하지 않으면, 나는 단지 위의 오류가 있지만 내가 그것을 포함하면 나는 4 배 더 많은 오류가있다. 난 이해가 안 돼요. 나는 그것을 아주 조심스럽게 시도했다. 나는 내가 어디 잘못되었는지 모른다. –