2009-04-25 4 views
1

明示的なスーパークラスコンストラクタを呼び出す継承されたクラスで、非常に不満を感じているようです。私は構文の権利を得ることができません!C++明示的スーパークラスコンストラクタヘッダーファイルの使用中に問題が発生する

これまで見たすべての例では、ヘッダーファイルを使って前方宣言からヘッダーとインラインクラス定義({} 'を使用)を分けることはありません。 .hと.ccファイルの間の構文をどのようにカバーするか。どんな助けもありがとう!ここで

は、コンパイラが私を与えるエラー(GCC)です:

serverconnection.h: In constructor "ServerConnection::ServerConnection(std::string, std::string)": serverconnection.h:25: error: expected `{' at end of input serverconnection.cc: At global scope: serverconnection.cc:20: error: redefinition of "ServerConnection::ServerConnection(std::string, unsigned int, short unsigned int, PacketSender*, int)" serverconnection.h:25: error: "ServerConnection::ServerConnection(std::string, unsigned int, short unsigned int, PacketSender*, int)" previously defined here serverconnection.cc: In constructor "ServerConnection::ServerConnection(std::string, std::string)": serverconnection.cc:20: error: no matching function for call to "Connection::Connection()"

私は接続(デフォルト接続のコンストラクタを呼び出すようにしようとしていることを理解し)、それはちょうど私の構文を理解していないよう。

ここコードである:

connection.h:

class Connection { 
    public: 
     Connection(string myOwnArg); 
}; 

connection.cc:

#include "connection.h" 
Connection::Connection(string myOwnArg) { 
    //do my constructor stuff 
} 

serverconnection.h:

#include "connection.h" 
class ServerConnection : public Connection { 
    public: 
     ServerConnection(string myOwnArg, string superClassArg) : Connection(superClassArg); 
}; 

serverconnection.ccは:

#include "serverconnection.h" 
#include "connection.h" 
ServerConnection::ServerConnection(string myOwnArg, string superClassArg) { 
    //do my constructor stuff 
} 

ありがとうございます!

答えて

5

初期化子リストをクラス宣言に入れるのではなく、関数の定義に入れます。ヘッダからそれを削除し、あなたの.ccファイル内:

#include "serverconnection.h" 
#include "connection.h" 

ServerConnection::ServerConnection(string myOwnArg, string superClassArg) : Connection(superClassArg) { 
    //do my constructor stuff 
} 
+0

ありがとうございます!私はあなたがジェフを13秒遅らせると思うので、私はあなたの答えを受け入れられたとマークします(申し訳ありませんジェフ!) –

+0

ありがとう!何らかの理由で、大部分のC++チュートリアルでは、別々のhおよびccファイルを使用しているときの状態を表示していません。 – jotadepicas

2

あなたは、サーバーconnection.ccにserverconnection.hから基底クラスの初期化リストを移動する必要があります。

ServerConnection::ServerConnection(string myOwnArg, string superClassArg) 
    : Connection(superClassArg) { 
    //do my constructor stuff 
} 

そして、ちょうど宣言ServerConnecitonコンストラクタ。ヘッダーにデコレーションがありません。

+0

今すぐ完璧に動作します! –

0

あなたのクラス宣言の終わりにセミコロンを逃している:いくつかの非常に紛らわしいエラーメッセージにつながることができ、およびコンパイラがファイルにエラーを置かないだろう

class Connection { 
    public: 
     Connection(string myOwnArg); 
}; // semicolon here 

忘却を本当にエラーです。

あなたのコンストラクタ宣言/定義にメンバー初期化リストを提供する場合、それらの中カッコにコードを入れない場合でも、実装の残りの部分に中カッコを指定する必要があります。宣言の一部ではなく、メンバ初期化リストを定義の一部として考えることができます。

+0

本当にありがとうございました。それは、実際のコードがはるかに大規模なので、カットアンドペーストの問題でした。しかし、良い目! –

関連する問題