472,796 Members | 2,212 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,796 software developers and data experts.

"was not declared in this scope"

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
5 27861
"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
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Brett | last post by:
I get the following in my watch window on the strType var for the second to last line: strType error: identifier 'strType' out of scope string strName; string strId; string strvalue; string...
0
by: Steven Bethard | last post by:
Felipe Almeida Lessa wrote: > Em Sex, 2006-04-14 ąs 09:31 -0600, Steven Bethard escreveu: >> Here's the code I used to test it. >> >> >>> def make(callable, name, args, block_string): >> ... ...
33
by: Sunny | last post by:
Hi, Sometime, when your script is too big, IE Gives you a warning "Stop Running This Script" A script on this page is causing Internet Explorer to run slowly. Does anyone knows, How to...
5
by: sfuo | last post by:
Hey guys, I went from working with SDL to OpenGL now and have transferred some code from one project to another. I do not understand why I am getting an error with this: "lib.h" #ifndef...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.