无法使操作员过载>&燃气轮机;在c++; 我在C++的学习过程中,但我尝试从java代码到C++进行翻译时,一直犯这个错误。我有一些java类,我必须把它们转换成C++代码(使用C++风格编程)。我有一个Article类,ArticleUnitaire类继承自Article,还有一个Ramete类继承自ArticleUnitaire。所有的吸收器和设置器都工作得很好,问题是当我尝试做与C++一样的ToStin时,操作符一些问题:
Stylo::getCouleur()不是常量无法使操作员过载>&燃气轮机;在c++; 我在C++的学习过程中,但我尝试从java代码到C++进行翻译时,一直犯这个错误。我有一些java类,我必须把它们转换成C++代码(使用C++风格编程)。我有一个Article类,ArticleUnitaire类继承自Article,还有一个Ramete类继承自ArticleUnitaire。所有的吸收器和设置器都工作得很好,问题是当我尝试做与C++一样的ToStin时,操作符一些问题:,c++,overloading,operator-keyword,C++,Overloading,Operator Keyword,Stylo::getCouleur()不是常量 Ramete::getGrammage()不是常量 这是我最先发现的两个超负荷操作。我没有试着通读代码,但是第一个想法,你包括在内了吗?那么为什么getReference不是const呢?你的“getReference”应该是const。是的,关于getReference你是绝对正确的,我只是错过了它,因为其他类似的函数都是const,对不起,我的不好。然而,我试图包括iostream,这使错误数量增加了三倍。显然,如果我不包括它,我只有上面的错
这是我最先发现的两个超负荷
操作。我没有试着通读代码,但是第一个想法,你包括在内了吗?那么为什么getReference
不是const呢?你的“getReference”应该是const。是的,关于getReference你是绝对正确的,我只是错过了它,因为其他类似的函数都是const,对不起,我的不好。然而,我试图包括iostream,这使错误数量增加了三倍。显然,如果我不包括它,我只有上面的错误,但如果我包括它,我有4倍多的错误。我不明白。我试着非常小心地做这件事。我不知道我错在哪里。是的,但我必须使用is作为成员函数,因为我需要在子类中重新定义它。例如,在Stylo中,我从Article类(如java中的super)调用Afficher方法,然后连接子类中的特定类型。例如,在Stylo类中,我显示Article类中的Afficher方法,并且我还想添加couler属性。这就像“超级连接”与“couleur=getcouleur()”用更多细节编辑了我的答案。我不明白:既然在源代码中定义了Afficher,为什么要将它作为虚拟的?这样派生方法就会被一条语句调用,如art.Afficher(os)代码>,即使对编译器来说,art
看起来只是一篇文章
。它不起作用。现在我有更多的错误。我真的不知道该怎么办了。。。
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;
}
}
#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;
};
#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;
};
...
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]’
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...
}