C++ classes with members referencing each other

十年热恋 提交于 2019-12-20 03:31:46

问题


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!


回答1:


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




回答2:


  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



回答3:


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;



回答4:


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"



来源:https://stackoverflow.com/questions/8140713/c-classes-with-members-referencing-each-other

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!