2010-02-11 110 views
2

我很高興在C++中工作,直到編譯時間到達。我有幾個類在一些命名空間(讓我們稱之爲N);其中兩個類對應一個基類,另一個類從它派生。每個類都有自己的一對.hpp和.cpp文件;我認爲它會是這樣的:從C++中的相同名稱空間中的類繼承的問題

namespace N{ 

    class Base{ 
    }; 

    class Derived: public Base{ 
    }; 

} 

然而,G ++(也許連接器)不斷告訴我:

Derived.hpp:n: error: expected class-name before ‘{’ token 

它不承認基地作爲一個階級,甚至當我有正確的#include '將與其定義相對應的hpp文件編輯爲Derived的.hpp!

「這件事情與#包括」,我想,因爲這些類.hpps在其他文件#included,以便我在Derived.hpp將此添加到衍生聲明:

#include "Base.hpp" 
namespace N{ 

    class Base; 

    class Derived: public Base{ 
    }; 
} 

而現在G ++抱怨:

Derived.hpp:n: error: invalid use of incomplete type ‘struct N::Base’ 

所以,我迷路了。請幫助我,我會做很多工作。 (順便說一下,我對Python非常有經驗,而不是C++,所以這個問題對我來說真的很奇怪,而且我改變了類的名字和東西:)。

編輯:我的文件更準確的表示是:

File Pieza.hpp 
----------------------- 
#include "Celda.hpp" 

namespace Reglas 
{ 
    class Pieza 
    {  
     public: 
     Pieza() {} 
     virtual ~Pieza() {} 

     private: 
     Celda *c; 
    }; 
} 


File Jugador.hpp 
----------------------- 
#include "Jugada.hpp" 
#include "Excepciones.hpp" 
#include "Pieza.hpp" 
namespace Reglas 
{ 
//compiler asked for these :S 
class Celda; 
class Tablero; 
    class Jugador : public Pieza 
    { 
     public: 
     Jugador() {} 
     virtual ~Jugador() {} 
    }; 
} 
+1

首先,這些是編譯器錯誤,而不是鏈接器錯誤。鏈接器在編譯後發生,並且尚未通過。其次,告訴我們你的文件是如何組織的,這兩個類是在同一個頭文件中定義的?你是否正確地將它們包含在實現文件中? – 2010-02-11 18:41:44

+2

你不是以智能的方式提出這個問題。你張貼你認爲應該編譯的內容。我們同意,它應該。你沒有發佈你的頭文件的真實內容,也沒有辦法從你的片段中看出他們有什麼問題。 – 2010-02-11 18:56:17

+0

謝謝,我已經編輯了我的問題。 – Fabzter 2010-02-11 18:59:40

回答

3
Derived.hpp:n: error: invalid use of incomplete type ‘struct N::Base’ 

這讓我覺得,你沒有#include "Base.hppDerived.cpp源文件中。

編輯:在您的Derived.cpp,嘗試改變的#include s量級到:

#include "base.hpp" 
#include "derived.hpp" 

// .. rest of your code .. 

像這樣:

// Derived.hpp 
#pragma once 

namespace foo 
{ 
    class Base; 

    class Derived : public Base 
    { 
    public: 
     Derived(); 

     ~Derived(); 
    }; 
} 

// Derived.cpp 
#include "base.hpp" 
#include "derived.hpp" 

namespace foo 
{ 
    Derived::Derived() 
    { 
    } 

    Derived::~Derived() 
    { 
    } 
} 

所以,你要編輯Jugador.hpp看起來像這樣:

// Jugador.hpp 
#include "Pieza.hpp" // move this above Jugada.hpp 
#include "Jugada.hpp" 
#include "Excepciones.hpp" 
namespace Reglas 
{ 
//compiler asked for these :S 
class Celda; 
class Tablero; 
    class Jugador : public Pieza 
    { 
     public: 
     Jugador() {} 
     virtual ~Jugador() {} 
    }; 
} 
+0

或者包含的內容很混亂,在這裏更有可能 – Joshua 2010-02-11 19:34:37

+0

我確實包括了它:( – Fabzter 2010-02-11 19:35:21

+0

我已經更新了我的答案,希望它對我有幫助 – xian 2010-02-11 21:36:30

3

你的文件應該是這樣的:

File Base.hpp 
----------------------- 
namespace N 
{ 
    class Base 
    {  
     public: 
     Base() {} 
     virtual ~Base() {} // Make sure you have a virtual destructor 
    }; 
} 


File Derived.hpp 
----------------------- 
#include "Base.hpp" 
namespace N 
{ 
    class Derived : public Base 
    { 
     public: 
     Derived() {} 
     ~Derived() {} 
    }; 
} 
+0

@mmyers - 感謝您糾正我的語法 – 2010-02-11 19:53:48

+0

沒問題 - 希望我的編輯摘要不會因爲居高臨下而出現,我這樣做是爲了讓非母語人士可以從錯誤中學習,而不必依賴編輯人員時間 – 2010-02-11 22:51:22