2017-04-16 211 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

相关问题