473,385 Members | 1,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 27949
"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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.