By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,223 Members | 1,463 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,223 IT Pros & Developers. It's quick & easy.

"was not declared in this scope"

P: n/a
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.

Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"William" <wh******@student.cs.uwaterloo.ca> wrote in message
news:Pi******************************@cpu02.studen t.cs.uwaterloo.ca...
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
Jul 23 '05 #2

P: n/a
On 2005-03-20, William <wh******@student.cs.uwaterloo.ca> wrote:
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,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 23 '05 #3

P: n/a
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.

On Mon, 21 Mar 2005, David White wrote:
"William" <wh******@student.cs.uwaterloo.ca> wrote in message
news:Pi******************************@cpu02.studen t.cs.uwaterloo.ca...
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

Jul 23 '05 #4

P: n/a
"William" <wh******@student.cs.uwaterloo.ca> wrote in message
news:Pi*******************************@cpu02.stude nt.cs.uwaterloo.ca...
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
Jul 23 '05 #5

P: n/a
William schrieb:
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
Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.