Hi, i'm trying to make a function dessinerFusee() to cout something with the operator<< but I have this "declaration is incompatible with" error coming from dessinerFusee(ostream& out)in my Fusee.cpp
In your header you need to properly scope the std ostream class you should only have one of the overload, either the friend or the "global" definition.
Also that Fusee parameter should be const qualified.
You should make a minimum example that's actually compilable that reproduces your error. Many times in doing this, you yourself will find the problem. And if you still don't, it makes it easier for us to help.
1 2 3 4 5 6 7 8 9
for (int i = 0; i < nHauteur - 2; i++)
{
out << "|";
for (int j = 0; j < nLargeur - 2; j++)
{
cout << " ";
}
out << "|\n";
}
Is the use of cout here intentional? (This is not probably related to your actual error)
#include <iostream>
#include <string>
#ifndef FUSEE_H
#define FUSEE_H
using std::ostream;
class Fusee
{
int m_numEtage;
int nLargeur;
int nHauteur;
bool m_allume;
public:
Fusee(int numEtage);//constructor qui ressoit le nombre d'étage de la fusée
void allume(bool a);
void dessinerFusee(ostream& out);
friend ostream& operator<<(ostream& out, Fusee& f);
};
ostream& operator<<(ostream& out, Fusee& f);
void Fusee::dessinerFusee(ostream& out)
{
for (int i = 0; i < m_numEtage; i++)
{
for (int i = 0; i < nLargeur - 2; i++)
{
out << "-";
}
out << "+\n";
for (int i = 0; i < nHauteur - 2; i++)
{
out << "|";
for (int j = 0; j < nLargeur - 2; j++)
{
std::cout << " ";
}
out << "|\n";
}
out << "+";
for (int i = 0; i < nLargeur - 2; i++)
{
out << "-";
}
out << "+\n";
}
}
ostream& operator<<(ostream& out, Fusee& f)
{
f.dessinerFusee(out);
return out;
}
#endif
int main()
{
}
So I don't think the code you're posting shows the full picture. Instead of posting even more code, try to condense everything into a single file that still reproduces the error you're having.
jlb,
Wouldn't the error the OP gets be "ostream was not declared" or something like that? My guess is he has "using namespace std;" thrown in a place before he does the #include.
Also, none of his code he has shown is using const, so it isn't clear that's the problem either. But I agree it could potentially be the problem, since the full picture is not being shown.
Wouldn't the error the OP gets be "ostream was not declared" or something like that? My guess is he has "using namespace std;" thrown in a place before he does the #include.
Maybe, but since the OP only posted a small portion of one error message we are both guessing as to the exact error. It is possible that there are more errors/warnings that are prior to the message "quoted" in which case there is really no telling what is happening.
Edit:
Also, none of his code he has shown is using const, so it isn't clear that's the problem either.
Correct it is probably not causing a problem, but using proper const correctness is always a good practice that should always be practiced.
Edit 2: Also the small example program should be using the affected code, especially if there are templates involved.