0

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.

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

#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

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
Tommy Andersen
  • 7,165
  • 1
  • 31
  • 50
asilvester635
  • 81
  • 2
  • 13
  • Even if you manage to solve your syntax error. Your program will still "not work". Have you seen [Why templates can only be implemented in header file](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file)? – WhiZTiM Apr 16 '17 at 19:15
  • 1
    Also, please don't use the deprecated throw specification. They're deprecated for a reason. – Rakete1111 Apr 16 '17 at 19:17

1 Answers1

2

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.

See Why can templates only be implemented in the header file?.

Other Issues I Noticed

  • 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> { ... };
    
Community
  • 1
  • 1
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • I disagree with putting the implementation in the header. That is not the idea of a header and thus should be avoided if possible. It is clearly possible if the template class does only need to take a small amount of allowed template parameters, usually done with a file that is called like `MyClass_Specialization.h` for a class MyClass which instantiates the templates and is included at the bottom of the source file. As in the accepted answer within the very link you posted. Since you have a lot of score on SO, I assume that you know this, but I'd recommend to add that in your answer. – Aziuth Apr 16 '17 at 20:00
  • 1
    @Aziuth, I realize that it is possible to put implementations of class templates in a .cpp file but I don't recommend the practice to beginners. – R Sahu Apr 16 '17 at 20:40
  • Could a non-templated subclass inherit from a templated super class? Using a templated class seems very complicated. My code worked perfectly without using a template. – asilvester635 Apr 16 '17 at 22:07
  • @asilvester635, yes, it can. Using templates (class templates as well as function templates) is definitely more complicated than a regular class or function. As you gain more experience, it will seem less complicated :) – R Sahu Apr 16 '17 at 22:26
  • @RSahu Thanks for the advice. – asilvester635 Apr 16 '17 at 23:29