"was not declared in this scope"

Discussion in 'C++' started by William, Mar 20, 2005.

  1. William

    William Guest

    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.
     
    William, Mar 20, 2005
    #1
    1. Advertising

  2. William

    David White Guest

    "William" <> wrote in message
    news:p...
    > 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
     
    David White, Mar 21, 2005
    #2
    1. Advertising

  3. On 2005-03-20, William <> 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/
     
    Donovan Rebbechi, Mar 21, 2005
    #3
  4. William

    William Guest

    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" <> wrote in message
    > news:p...
    > > 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
    >
    >
    >
     
    William, Mar 21, 2005
    #4
  5. William

    David White Guest

    "William" <> wrote in message
    news:p...
    > 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
     
    David White, Mar 21, 2005
    #5
  6. 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
     
    Malte Starostik, Mar 21, 2005
    #6
  7. William

    jun

    Joined:
    Mar 11, 2007
    Messages:
    1
    /***** 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
     
    jun, Mar 11, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Goodsell
    Replies:
    0
    Views:
    444
    Kevin Goodsell
    Sep 21, 2003
  2. Jack Klein
    Replies:
    0
    Views:
    731
    Jack Klein
    Sep 22, 2003
  3. Vaxius
    Replies:
    3
    Views:
    3,507
  4. Darren L. Weber
    Replies:
    14
    Views:
    1,689
    Pete Becker
    Jul 24, 2006
  5. Krice
    Replies:
    4
    Views:
    486
    Howard
    Oct 2, 2006
Loading...

Share This Page