Reputation: 427
I am in the learning process of C++ but I keep having this error when I try do do a translation from a java code to c++. I have some classes in java which I have to translate into c++ code (using the C++ style programming). I have an Article class, ArticleUnitaire class which inherits from Article, and a class Ramette which inherits from ArticleUnitaire. all the getters and setters work just fine, the problem is when I try to do the toString equivalent to c++, which is the operator<< overloaded. Let me show you the code and the errors: Java code:
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;
}
}
Here is my articles.h file:
#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;
};
And articles.cc file:
#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;
};
...
However, when I try to compile, I keep having the same error for the method 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]’
As you cand see, the compiler does not like what I am doing with the method Afficher which is used in the operator>> overloading. What am I doing wrong?
Upvotes: 0
Views: 434
Reputation: 72356
Overload operator<<
as a non-member function, not a member function. This is the only way to do it since you want your object to be on the right side of the operator, not the left side (and you can't change class std::ostream
).
EDIT: But that non-member function can in turn call a virtual member function...
Header files:
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;
}
Source files:
void Article::Afficher(std::ostream& os) const {
//...
}
void ArticleUnitaire::Afficher(std::ostream& os) const {
Article::Afficher(os);
//Other data...
}
Upvotes: 0
Reputation: 1734
Some problems:
Stylo::getCouleur() isn't const
Ramette::getGrammage() isn't const
These are the two first ones I've found
Upvotes: 1