2013-09-25 50 views
0

我想用C++和模板編寫容器類。但是,我有一個編譯錯誤,我不明白...與模板的類的構造函數的問題

變量elems是私人向量,聲明是:

private: 
    vector<DataType> elems; 

的載體是一個自定義的矢量。它的構造是:

vector::vector(int init_capacity) : vect_capacity(init_capacity), vect_size(0), vect_elems(NULL){ 
    assert(init_capacity >= 0); 

    if (init_capacity > 0){ 
    vect_elems = new Object[init_capacity]; 
} 

}

的構造可以如下圖所示:

template <class DataType> 
bag<DataType>::bag(int init_capacity) : elems(init_capacity) { 
} 

此代碼返回以下錯誤:

../src/vector.h: In instantiation of ‘vector<DataType>::vector(int) [with DataType = int]’: 
../src/bag.h:33:60: required from ‘bag<DataType>::bag(int) [with DataType = int]’ 
../src/bag_test.cpp:6:17: required from here 

老實說,我不知道是什麼可能會發生。將非常感謝任何人可以指出我在正確的方向...

+0

那是整個錯誤消息嗎? – 0x499602D2

+0

這只是錯誤消息的一部分。 –

+0

這是唯一的錯誤。一些警告在這裏和那裏......但不相關。我正在使用'bag b1(10)''在測試程序中使用這個命令調用這個函數......然後它出現這個錯誤。 – zorman2000

回答

1

對不起,這個非常愚蠢的問題。確實,編譯器確實抱怨這個,但代碼實際上編譯。感謝@WhozCraig和@ n.m堅持認爲這不是一個錯誤,我注意到它實際上正在建設中。謝謝!爲了將來的參考,我確實發佈了整個消息:

**** Build of configuration Debug for project ADS **** 
make all 
Building file: ../src/bag_test.cpp 

Invoking: GCC C++ Compiler 
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/bag_test.d" -MT"src/bag_test.d" -o "src/bag_test.o" "../src/bag_test.cpp" 
In file included from ../src/bag_test.cpp:2:0: 
../src/bag.h:23:66: warning: friend declaration ‘std::ostream& operator<<(std::ostream&, const bag<DataType>&)’ declares a non-template function [-Wnon-template-friend] 
../src/bag.h:23:66: note: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) 
In file included from ../src/bag.h:2:0, 
       from ../src/bag_test.cpp:2: 
../src/vector.h: In instantiation of ‘vector<DataType>::vector(int) [with DataType = int]’: 
../src/bag.h:34:53: required from ‘bag<DataType>::bag(int) [with DataType = int]’ 
../src/bag_test.cpp:6:17: required from here 
../src/vector.h:100:6: warning: ‘vector<int>::vect_capacity’ will be initialized after [-Wreorder] 
../src/vector.h:99:6: warning: ‘int vector<int>::vect_size’ [-Wreorder] 
../src/vector.h:108:1: warning: when initialized here [-Wreorder] 
Finished building: ../src/bag_test.cpp 

Building target: ADS 
Invoking: GCC C++ Linker 
g++ -o "ADS" ./src/bag_test.o 
Finished building target: ADS 

**** Build Finished **** 
+3

注:我從不堅持沒有錯誤。我堅持說你沒有發佈*一個。我仍然堅持認爲你錯過了粘貼。我真的沒有看到這個警告的結果。 – WhozCraig

+0

嗯,至少你堅持要我發佈錯誤,所以我注意到那裏沒有錯誤。謝謝! – zorman2000