"was not declared in this scope"

W

William

In Peer.h, I have:

class Peer {
// ...
};

In Overseer.h, I have:

#include "Peer.h"
#include <vector>

using namespace std;

class Overseer {
private:
vector<Peer> connectedPeers;

public:
vector<Peer> getConnectedPeers() {
return connectedPeers;
}

};

I got the following error when I attempted to compile my code:
In file included from Communications.h:19,
from Peer.h:4,
from Peer.cc:1:
Overseer.h:11: error: `Peer' was not declared in this scope
Overseer.h:11: error: template argument 1 is invalid
Overseer.h:11: error: template argument 2 is invalid
Overseer.h:11: error: ISO C++ forbids declaration of `connectedPeers' with
no
type
Overseer.h:14: error: `Peer' was not declared in this scope
Overseer.h:14: error: template argument 1 is invalid
Overseer.h:14: error: template argument 2 is invalid
Overseer.h:14: error: ISO C++ forbids declaration of `getConnectedPeers'
with
no type
make: *** [Peer.o] Error 1

In Overseer.h, it is very clear that I have #include "Peer.h".
1) What is the cause of the error?
2) Any suggestions for a fix is appreciated.

Thanks.
 
D

David White

William said:
In Peer.h, I have:

class Peer {
// ...
};

In Overseer.h, I have:

#include "Peer.h"
#include <vector>

using namespace std;

It's a bad idea to put this in a header file. It means that every file that
includes this header is forced to have all names in std:: visible at global
scope, and all it does here is save you a couple of "std:::" on your
vectors. Put it in files that aren't #included by other files if you like,
but not here.
class Overseer {
private:
vector<Peer> connectedPeers;

public:
vector<Peer> getConnectedPeers() {
return connectedPeers;
}

};

I got the following error when I attempted to compile my code:
In file included from Communications.h:19,
from Peer.h:4,
from Peer.cc:1:
Overseer.h:11: error: `Peer' was not declared in this scope
Overseer.h:11: error: template argument 1 is invalid
Overseer.h:11: error: template argument 2 is invalid
Overseer.h:11: error: ISO C++ forbids declaration of `connectedPeers' with
no
type
Overseer.h:14: error: `Peer' was not declared in this scope
Overseer.h:14: error: template argument 1 is invalid
Overseer.h:14: error: template argument 2 is invalid
Overseer.h:14: error: ISO C++ forbids declaration of `getConnectedPeers'
with
no type
make: *** [Peer.o] Error 1

In Overseer.h, it is very clear that I have #include "Peer.h".
1) What is the cause of the error?
2) Any suggestions for a fix is appreciated.

I can't explain the errors. It compiled by VC++ 6.0 with no errors for me. I
suspect that you haven't posted the entire contents of the header files and
inadvertently removed the cause of your errors.

DW
 
D

Donovan Rebbechi

In Peer.h, I have:

class Peer {
// ...
};

In Overseer.h, I have:

#include "Peer.h"
#include <vector>

using namespace std;

class Overseer {
private:
vector<Peer> connectedPeers;

public:
vector<Peer> getConnectedPeers() {
return connectedPeers;
}

};

I got the following error when I attempted to compile my code:
In file included from Communications.h:19,
from Peer.h:4,
from Peer.cc:1:
Overseer.h:11: error: `Peer' was not declared in this scope
Overseer.h:11: error: template argument 1 is invalid
Overseer.h:11: error: template argument 2 is invalid
Overseer.h:11: error: ISO C++ forbids declaration of `connectedPeers' with
no
type
Overseer.h:14: error: `Peer' was not declared in this scope
Overseer.h:14: error: template argument 1 is invalid
Overseer.h:14: error: template argument 2 is invalid
Overseer.h:14: error: ISO C++ forbids declaration of `getConnectedPeers'
with
no type
make: *** [Peer.o] Error 1

In Overseer.h, it is very clear that I have #include "Peer.h".

It's not very clear at all, because Peer.h could indirectly include
Overseer.h, so if this happens and you use include guards, it doesn't
get included.

Looks to me like Peer.cc includes Peer.h which includes
1) What is the cause of the error?

The author doesn't understandg about class dependencies
2) Any suggestions for a fix is appreciated.

Make sure you understand what depends on what (both in terms of class
definitions and declarations). Draw a diagram of it. Then make sure
your includes are consistent with the functional dependencies.

Cheers,
 
W

William

The entire contents of my Peer.h and Overseer.h are as follows:

/***** Peer.h *****/
#ifndef _Peer_h_
#define _Peer_h_

#include "Communications.h"

class Peer {
private:
int peerID;
string peerIP;
int peerPort;

public:
int getPeerID() {
return peerID;
}
string getPeerIP() {
return peerIP;
}
int getPeerPort() {
return peerPort;
}

void setPeerID( int ID ) {
peerID = ID;
}
void setPeerIP( string IP ) {
peerIP = IP;
}
void setPeerPort( int port ) {
peerPort = port;
}
};

#endif


/****** Overseer.h ********/
#ifndef _Overseer_h_
#define _Overseer_h_

#include "Peer.h"
#include <vector>

class Overseer {
private:
vector<Peer> connectedPeers;

public:
vector<Peer> getConnectedPeers() {
return connectedPeers;
}

};
#endif


Once again, the error message is:
g++ -g -c -o Overseer.o Overseer.cc
g++ -g -c -o Communications.o
Communications.cc
g++ Overseer.o Util.o Communications.o Parser.o -o
overseer -lsocket -lnsl
g++ -g -c -o Peer.o Peer.cc
In file included from Communications.h:19,
from Peer.h:4,
from Peer.cc:1:
Overseer.h:9: error: `Peer' was not declared in this scope
Overseer.h:9: error: template argument 1 is invalid
Overseer.h:9: error: template argument 2 is invalid
Overseer.h:9: error: ISO C++ forbids declaration of `connectedPeers' with
no
type
Overseer.h:12: error: `Peer' was not declared in this scope
Overseer.h:12: error: template argument 1 is invalid
Overseer.h:12: error: template argument 2 is invalid
Overseer.h:12: error: ISO C++ forbids declaration of `getConnectedPeers'
with
no type
make: *** [Peer.o] Error 1


Any suggestion to the cause of the error and a fix is greatly
appreciated.



William said:
In Peer.h, I have:

class Peer {
// ...
};

In Overseer.h, I have:

#include "Peer.h"
#include <vector>

using namespace std;

It's a bad idea to put this in a header file. It means that every file that
includes this header is forced to have all names in std:: visible at global
scope, and all it does here is save you a couple of "std:::" on your
vectors. Put it in files that aren't #included by other files if you like,
but not here.
class Overseer {
private:
vector<Peer> connectedPeers;

public:
vector<Peer> getConnectedPeers() {
return connectedPeers;
}

};

I got the following error when I attempted to compile my code:
In file included from Communications.h:19,
from Peer.h:4,
from Peer.cc:1:
Overseer.h:11: error: `Peer' was not declared in this scope
Overseer.h:11: error: template argument 1 is invalid
Overseer.h:11: error: template argument 2 is invalid
Overseer.h:11: error: ISO C++ forbids declaration of `connectedPeers' with
no
type
Overseer.h:14: error: `Peer' was not declared in this scope
Overseer.h:14: error: template argument 1 is invalid
Overseer.h:14: error: template argument 2 is invalid
Overseer.h:14: error: ISO C++ forbids declaration of `getConnectedPeers'
with
no type
make: *** [Peer.o] Error 1

In Overseer.h, it is very clear that I have #include "Peer.h".
1) What is the cause of the error?
2) Any suggestions for a fix is appreciated.

I can't explain the errors. It compiled by VC++ 6.0 with no errors for me. I
suspect that you haven't posted the entire contents of the header files and
inadvertently removed the cause of your errors.

DW
 
D

David White

William said:
The entire contents of my Peer.h and Overseer.h are as follows:

[snip]

I can't compile it as is because I don't have Communications.h, but if I
modify it minimally to exclude that then it still compiles okay. At first I
thought you might have an #ifdef instead of an #ifndef, but now I think that
circular inclusions, as Donovan Rebbechi's reply suggested, is the most
likely reason.

DW
 
M

Malte Starostik

William said:
The entire contents of my Peer.h and Overseer.h are as follows:

/***** Peer.h *****/
#ifndef _Peer_h_
#define _Peer_h_

Names starting _[A-Z] are reserved for the implementation, remove the
leading underscore or lowercase the P (although it's an extremely
commonplace convention to have preprocessor symbols in ALLCAPS). This
may even be your problem (unlikely though).
#include "Communications.h"

Some wild guesses, as you haven't posted Communications.h:
* Maybe the include guard for Communcations.h is _Peer_h_ (C&P mistake)?
* Maybe Communications.h directly or indirectly #includes Overseer.h?
class Peer {
private:
int peerID;
string peerIP;
int peerPort;

public:
int getPeerID() {
return peerID;
}
string getPeerIP() {
return peerIP;
}

getPeerIP() returns a copy of peerIP. Returning a const string& instead
would have saved that copy.
class Overseer {
private:
vector<Peer> connectedPeers;

public:
vector<Peer> getConnectedPeers() {
return connectedPeers;
}

Same here, but for a vector<Peer> the runtime/memory cost of the copy is
even higher than for a string with a dotted quad (assuming that's what
peerIP contains). Also, with the above, you cannot write:

Overseer boss;
for ( vector< Peer >::const_iterator it =
boss.getConnectedPeers().begin(); it != boss.getConnectedPeers().end();
++it ) { /*...*/ }

While with a const ref you can.
Once again, the error message is:
g++ -g -c -o Overseer.o Overseer.cc
g++ -g -c -o Communications.o
Communications.cc
g++ Overseer.o Util.o Communications.o Parser.o -o
overseer -lsocket -lnsl
g++ -g -c -o Peer.o Peer.cc
In file included from Communications.h:19,
from Peer.h:4,
from Peer.cc:1:
Overseer.h:9: error: `Peer' was not declared in this scope
Overseer.h:9: error: template argument 1 is invalid
Overseer.h:9: error: template argument 2 is invalid
Overseer.h:9: error: ISO C++ forbids declaration of `connectedPeers' with

Having a look at the preprocessor output as the compiler sees it will
likely help you here (g++ -E Peer.cc)

Cheers,
Malte
 

jun

Joined
Mar 11, 2007
Messages
1
Reaction score
0
/***** Peer.h *****/
> #ifndef _Peer_h_
> #define _Peer_h_
> #include "Communications.h"
> class Peer {
> <<snip>>
> }

/***** Overseer.h *****/
> #ifndef _Overseer_h_
> #define _Overseer_h_
> #include "Peer.h"
> #include <vector>
> class Overseer {
> private:
> vector<Peer> connectedPeers;
>
> public:
> vector<Peer> getConnectedPeers() {
> return connectedPeers;
> }


/***** compling *****/
> g++ -g -c -o Overseer.o Overseer.cc
> g++ -g -c -o Communications.o Communications.cc
> g++ Overseer.o Util.o Communications.o Parser.o -o overseer -lsocket -lnsl
> g++ -g -c -o Peer.o Peer.cc
> In file included from Communications.h:19,
> from Peer.h:4,
> from Peer.cc:1:
> Overseer.h:9: error: `Peer' was not declared in this scope
> . . .

Fact;
1. compling Overseer.cc is OK
2. compling Communications.cc is OK
3. compling Peer.cc is NG.

When compling Peer.cc, Peer.cc include Peer.h, and Peer.h include Communications.h, and also Communications.h include Overseer.h.

Peer.cc(1)->Peer.h(4)->Communications.h(19)->Overseer.h->Peer.h

The Overseer.h need to include Peer.h and define the Peer class previously,
*but* _Peer_h_ already defined, so Peer class will not defined previously.

This is the reason why you fail to comple the Peer.cc.

Enjoy!

JUN
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,225
Members
46,815
Latest member
treekmostly22

Latest Threads

Top