2

I'm trying to write 2 classes with members that reference each other. I'm not sure if I'm doing something wrong or it's just not possible. Can anyone help me out here...

Source.cpp

#include "Headers.h"
using namespace std;

void main()
{
    Network* network = new Network();

    system("pause");
    return;
}

Headers.h

#ifndef Headers_h
#define Headers_h

#include <iostream>
#include <vector>
#include "Network.h"
#include "Router.h"

#endif

Network.h

#include "Headers.h"

class Network
{
protected:
    vector<Router> Routers;
};

Router.h

#include "Headers.h"

class Router
{
protected:
    Network* network;
public:
};

The errors I'm getting are:

error C2143: syntax error : missing ';' before '<'
error C2238: unexpected token(s) preceding ';'
error C4430: missing type specifier - int assumed.

I'm pretty sure I'm not missing any semicolons or stuff like that. The program works find if I take out one of the members. I tried finding similar questions and the solution was to use pointers, but that's what I'm doing and it does't seem to be working!

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Saad Imran.
  • 4,480
  • 2
  • 23
  • 33

4 Answers4

4
  1. Use include guards.
  2. Use Forward declaration

E.g.

Source.cpp

#include "Network.h"
// using namespace std; // bad idea unless it's a quickie test program

void main()
{
    Network* network = new Network();

    system("pause");
    return;
}

Network.h

#ifndef MY_NETWORK_H_INCLUDED
#define MY_NETWORK_H_INCLUDED

#include "Router.h"
#include <vector>
using std::vector;
// even better is:
//     typedef std::vector<Router> t_RouterVec;
//     t_RouterVec routers;

class Network
{
protected:
    vector<Router> Routers;
};

#endif // MY_NETWORK_H_INCLUDED

Router.h

#ifndef MY_ROUTER_H_INCLUDED
#define MY_ROUTER_H_INCLUDED

class Network;

class Router
{
protected:
    Network* network;
public:
};

#endif MY_ROUTER_H_INCLUDED

VERY BAD IDEA I consider this to be a smell. You're making everyone include everything. Now every time you change something in Network.h or Router.h you have to recompile (and ideally re-test) everything!

#ifndef Headers_h
#define Headers_h

#include <iostream>
#include <vector>
#include "Network.h"
#include "Router.h"

#endif
Aikufurr
  • 48
  • 4
Kashyap
  • 15,354
  • 13
  • 64
  • 103
  • Thanks, I tried the include guards before. They did not work so I used a common headers file that has an include guard. As for the forward decleration, that works for me. Thanks for your help! – Saad Imran. Nov 15 '11 at 17:55
4

first error - you need to explicitly use the namespace:

std::vector<Router> Routers;

Don't "use namespace std;" in header files

Other errors are restulting from the first :)

As to the referencing to the class defined later, you need to do forward declaration of Router class, put

class Router;

into your Network.h

Erbureth
  • 3,378
  • 22
  • 39
  • I took out the vector member and changed it to `Router* router;` and added the forward decleration in Headers.h. But still no luck. – Saad Imran. Nov 15 '11 at 17:51
  • Thanks, your fixed answer fixed the problem for me. I needed to add the forward decleration in Network.h and not Headers.h. Thanks for the help! – Saad Imran. Nov 15 '11 at 17:54
  • You need the full definition of `Router` in order to define an `std::vector`. A forward definition of `Network` in `Router.h` would be appropriate, however. – James Kanze Nov 15 '11 at 17:55
1

You need to prefix your vector with std::. Your other option is to put using namespace std; at the top of that file.

std::vector<Router> Routers;

or

using namespace std;
...
vector<Router> Routers;
TheBuzzSaw
  • 8,648
  • 5
  • 39
  • 58
  • Please do not use `using namespace std`, *especially* in a header file, since it will pollute any other file that includes it. – Peter Nov 15 '11 at 17:49
  • @TheBuzzSaw I caught that just before you posted, but that did not solve my problem. I tried changing the member to `Router* router;` but it still does not work. Same error. – Saad Imran. Nov 15 '11 at 17:49
  • @Peter I was not implying that he should put it in the header file. – TheBuzzSaw Nov 15 '11 at 17:50
  • @SaadImran. See the other answers. You are attempting to have two header files include each other. Resolve that first. – TheBuzzSaw Nov 15 '11 at 17:51
  • @TheBuzzSaw I got forward decleration to work for me. However, I can't see how two header files are including each other, curious to know. Can you point that out for me? Thanks. – Saad Imran. Nov 15 '11 at 17:57
  • `Headers.h` is including `Network.h`, and `Network.h` is including `Headers.h`. Generally, don't have an "omniheader" that just includes all the things. Keep the classes isolated as much as possible. – TheBuzzSaw Nov 15 '11 at 18:05
0

Compilation issues:

  1. Please add std:: before vector in Network.h

  2. Please Correct main to return an integer, as per standards.


Circular References:

Network.h references class Router for its vector contents and Router.h references class Network. This is a chicken an egg scenario. You can't have both :).

To resolve this I would recommend the following:

  1. Add an opaque class reference Network in Router.h.

    Since Router.h uses a pointer to a Network, it doesn't need to know the details of Network, it just needs to know that network is a class.

    class Network;

  2. Change the order of the include directives in Headers.h to be

    #include "Router.h"

    #include "Network.h"

John Rocha
  • 1,656
  • 3
  • 19
  • 30