2017-04-16 214 views
0

我使用sublimetext3在C++中進行編程。我的程序有一個名爲Array的超類,和一個名爲IntArray的子​​類。這兩個類都是模板類。目前,我無法編譯該程序。它一直在我的IntArray.cpp文件中給我一個錯誤,特別是在我的基類的構造函數中,我使用基類的構造函數的參數調用並初始化超類的構造函數。我不知道如何從子類的模板構造函數中調用超類的模板構造函數。錯誤消息如下所示。此外,錯誤消息下面是main.cpp,Array.cpp,Array.h,IntArray.cpp,IntArray.h和Makefile的源代碼文件。該計劃尚未完成。我目前只有一個獲取數組大小的方法。從終端如何從C++中的模板基類的構造函數調用模板超類的構造函數?

錯誤消息:

IntArray.cpp:4:56: error: member initializer 'Array' does not name a non-static data member or base class 
template<class T> IntArray<T>::IntArray(T s) throw() : Array(s) { 
                 ^~~~~~~~ 

1 error generated. 

的main.cpp

#include <iostream> 
#include <string> 
#include "Array.h" 
#include "IntArray.h" 

int main(int argc, char** argv) { 

    // make an array of doubles with size 10 
    Array<int> iA(10); 

    // get the size of the array 
    std::cout<< "The size of IntArray is" <<iA.getSize()<<std::endl; 

} // end of main 

Array.cpp

#include "Array.h" 

// constructor 
template<class T> Array<T>::Array(T s) throw() { 
    size = s; 
} 

// destructor 
template<class T> Array<T>::~Array() throw() { 

} 

// getter methods 
template<class T> T Array<T>::getSize() const throw() { 
    return size; 
} 

Array.h

#ifndef ARRAY_H 
#define ARRAY_H 

template<class T> class Array { 
private: 
    T size; 

public: 
    Array(T s) throw(); 
    virtual ~Array() throw(); 
    // getter methods that throws an exception if the index is out of bounds 
    T getSize() const throw(); 


    // setters that throws an exception if the index is out of bounds 
}; 

#endif 

IntArray.cpp

#include "IntArray.h" 

// constructor 
template<class T> IntArray<T>::IntArray(T s) throw() : Array(s) { 

} 

// desctructor 
template<class T> IntArray<T>::~IntArray() throw() { 

} 

IntArray.h

#ifndef INTARRAY_H 
#define INTARRAY_H 
#include "Array.h" 

template<class T> class IntArray : public Array<T> { 

public: 
    IntArray(T s) throw(); 
    virtual ~IntArray() throw(); 
    //int getSize() const throw(); 
}; 

#endif 

生成文件

all:main 

main.o: main.cpp Array.h IntArray.h 
    g++ -c -Werror main.cpp 

Array.o: Array.cpp Array.h 
    g++ -c -Werror Array.cpp 

IntArray.o: IntArray.cpp IntArray.h 
    g++ -c -Werror IntArray.cpp 

main: main.o Array.o IntArray.o 
    g++ -o main main.o Array.o IntArray.o 
+0

即使你設法解決您的語法錯誤。你的程序仍然「不起作用」。你見過[爲什麼模板只能在頭文件中實現](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file)? – WhiZTiM

+1

另外,請不要使用棄用的throw規範。他們由於某種原因而被棄用。 – Rakete1111

回答

1

使用

template <class T> IntArray<T>::IntArray(T s) throw() : Array<T>(s) {} 
                 // ^^^ Use <T> 

更重要的是,把implemetation也.h文件。

請參閱Why can templates only be implemented in the header file?

其他問題,我注意到

  • 它不會讓您使用T s的尺寸感。 std::size_t s更有意義。
  • IntArray是一個類模板沒有任何意義。它使我更有意義的使用方法:

    class IntArray : public Array<int> { ... }; 
    
+0

我不同意將實現放在標題中。這不是一個標題的想法,因此應儘可能避免。如果模板類只需要獲取少量允許的模板參數,通常使用一個名爲MyClass_Specialization.h的文件來實現一個類MyClass,該類實例化模板並且包含在源文件。正如您發佈的鏈接中的接受答案一樣。由於你在這方面有很多分數,我認爲你知道這一點,但我建議在你的答案中加上。 – Aziuth

+0

@Aziuth,我意識到可以將類模板的實現放在.cpp文件中,但我不建議練習初學者。 –

+0

非模板化的子類是否可以從模板化的超類繼承?使用模板類看起來非常複雜。我的代碼工作完美,沒有使用模板。 – asilvester635

相關問題