2016-05-12 9 views
0

私は2つのクラスを持っています:User.hとRoom.hは両方とも、他のクラスのオブジェクトへのポインタを含んでいます。 私は.hファイルを含める方法を理解していると思いますが、まだ私の.cppファイル(user.cpp)のいずれかでエラーが発生します。2つのクラスは互いにメンバーを持ちます

user.h

#ifndef USER_H 
#define USER_H 

class Room; 

using namespace std; 

class User 
{ 
private: 
    Room* _currRoom; 
public: 
    //some functions... 
}; 

#endif 

room.h私はこの問題は何ですかuser.cpp でroom.cppとroom.hでuser.hを含ま

#ifndef ROOM_H 
#define ROOM_H 

#include "User.h" 

class Room 
{ 
private: 
    vector<User*> _users; 
    User* _admin; 
    int _maxUsers; 
    int _questionTime; 
    int _questionsNo; 
    string _name; 
    int _id; 

public: 
    Room(int id, User* admin, string name, int maxUsers, int questionsNo,int questionTime); 
    //more functions... 
}; 

#endif 

私は何をしましたか?

+2

「cpp」ファイルを含めないでください。 – Rakete1111

+5

エラーがある場合は、それらを共有する必要があります。エラーテキストが大好きです。 – NathanOliver

+0

エラーエラーC2514: '部屋':クラスにコンストラクタがありません。私はuser.cppでコンストラクタを呼び出しました。 – abcdef123

答えて

0

room.hには、代わりに#include "User.h"を前方宣言に置き換えます。 .hファイルで宣言を前方に使用し、.cppファイルに対応する#include文を動かす:

user.h

#ifndef USER_H 
#define USER_H 

class Room; 

using namespace std; 

class User 
{ 
private: 
    Room* _currRoom; 
public: 
    //some functions... 
}; 

#endif 

user.cpp

#include "user.h" 
#include "room.h" 
... 

room.h

#ifndef ROOM_H 
#define ROOM_H 

#include <vector> 
#include <string> 

class User; 

class Room 
{ 
private: 
    vector<User*> _users; 
    User* _admin; 
    int _maxUsers; 
    int _questionTime; 
    int _questionsNo; 
    string _name; 
    int _id; 

public: 
    Room(int id, User* admin, string name, int maxUsers, int questionsNo, int questionTime); 
    //more functions... 
}; 

#endif 

room.cpp

#include "room.h" 
#include "user.h" 
... 

このように前方宣言をしないと、別のヘッダ(AにはAを含むBを含む)、したがってヘッダーを介して間接的にヘッダが含まれる循環的な状況になりますガードがすでに定義されているため、宣言の処理が妨げられ、エラーが発生します。

+0

ありがとうございました!今私は続けることができます – abcdef123

関連する問題