declaration is incompatible with...

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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++)
			{
				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;
}

And this is my Fusee.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>

#ifndef FUSEE_H
#define FUSEE_H

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 largeur(int nLargeur);//nouvelle largeur d'un étage
	void hauteur(int nHauteur);//nouvelle hauteur d'un étage
	void allume(bool a);
	void dessinerFusee(ostream& out);
	
	friend ostream& operator<<(ostream& out, Fusee& f);
};
ostream& operator<<(ostream& out, Fusee& f);

#endif 
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)

My minimal example compiles:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#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.
Last edited on
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.
Last edited on
Thx both of you. It did the trick!
What exactly "did the trick"?
Declaring ostream with: using std::ostream in the .h.
Interesting. Thanks for the response.
Topic archived. No new replies allowed.