[英]How to call a template super class's constructor from a template base class's constructor in c++?
I'm programming in c++ using sublimetext3.我正在使用 sublimetext3 在 C++ 中编程。 My program has a superclass called Array, and a subclass called IntArray.我的程序有一个名为 Array 的超类和一个名为 IntArray 的子类。 Both classes are template classes.这两个类都是模板类。 Currently, I'm having trouble compiling the program.目前,我在编译程序时遇到问题。 It keeps giving me an error in my IntArray.cpp file, specifically in my base class's constructor where I call and initialize the superclass's constructor with the parameter of the base class's constructor.它一直在我的 IntArray.cpp 文件中给我一个错误,特别是在我的基类的构造函数中,我用基类的构造函数的参数调用和初始化超类的构造函数。 I'm not sure how to call a superclass's template constructor from a subclass's template constructor.我不确定如何从子类的模板构造函数调用超类的模板构造函数。 The error message is shown below.错误消息如下所示。 Also, below the error message are my source code files for main.cpp, Array.cpp, Array.h, IntArray.cpp, IntArray.h, and Makefile.此外,错误消息下方是我的 main.cpp、Array.cpp、Array.h、IntArray.cpp、IntArray.h 和 Makefile 的源代码文件。 The program is not yet complete.该计划尚未完成。 I currently only have one method that gets the size of the array.我目前只有一种获取数组大小的方法。
Error message from terminal:来自终端的错误消息:
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数组.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数组.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 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数组.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
Makefile生成文件
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
Use用
template <class T> IntArray<T>::IntArray(T s) throw() : Array<T>(s) {}
// ^^^ Use <T>
More importantly, put the implemetation also in the .h file.更重要的是,将实现也放在 .h 文件中。
See Why can templates only be implemented in the header file?请参阅为什么模板只能在头文件中实现? . .
Other Issues I Noticed我注意到的其他问题
T s
for size.您使用T s
表示大小是没有意义的。 std::size_t s
makes more sense. std::size_t s
更有意义。 It does not make sense that IntArray
is a class template. IntArray
是一个类模板是没有意义的。 It makes more sense to me to use:对我来说使用更有意义:
class IntArray : public Array<int> { ... };
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.