如何在C ++中从模板基类的构造函数调用模板超类的构造函数? [英] How to call a template super class's constructor from a template base class's constructor in c++?

查看:115
本文介绍了如何在C ++中从模板基类的构造函数调用模板超类的构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用sublimetext3在c ++中进行编程.我的程序有一个名为Array的超类和一个名为IntArray的子​​类.这两个类都是模板类.目前,我在编译程序时遇到问题.它一直在我的IntArray.cpp文件中给我一个错误,特别是在我的基类的构造函数中,我用基类的构造函数的参数调用并初始化超类的构造函数.我不确定如何从子类的模板构造函数调用超类的模板构造函数.错误消息如下所示.另外,错误消息下方是main.cpp,Array.cpp,Array.h,IntArray.cpp,IntArray.h和Makefile的源代码文件.该程序尚未完成.我目前只有一种方法可以获取数组的大小.

I'm programming in c++ using sublimetext3. My program has a superclass called Array, and a subclass called 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. 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. The program is not yet complete. I currently only have one method that gets the size of the array.

来自终端的错误消息:

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

推荐答案

使用

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

更重要的是,将实现也放在.h文件中.

More importantly, put the implemetation also in the .h file.

请参见为什么只能在头文件?.

我注意到的其他问题

  • 您使用 T s 来确定大小没有意义. std :: size_t s 更有意义.
  • IntArray 是类模板没有任何意义.使用它对我来说更有意义:

  • It does not make sense that you are using T s for size. std::size_t s makes more sense.
  • It does not make sense that IntArray is a class template. It makes more sense to me to use:

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

这篇关于如何在C ++中从模板基类的构造函数调用模板超类的构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆