Ich programmiere in C++ mit sublimetext3. Mein Programm hat eine Oberklasse namens Array und eine Unterklasse namens IntArray. Beide Klassen sind Vorlagenklassen. Zurzeit habe ich Probleme, das Programm zu kompilieren. Es gibt mir einen Fehler in meiner IntArray.cpp-Datei, speziell im Konstruktor meiner Basisklasse, wo ich den Konstruktor der Superklasse mit dem Parameter des Konstruktors der Basisklasse aufruft und initialisiere. Ich bin mir nicht sicher, wie ich den Template-Konstruktor einer Superklasse aus dem Template-Konstruktor einer Unterklasse aufrufen kann. Die Fehlermeldung wird unten angezeigt. Unter der Fehlermeldung befinden sich auch meine Quellcodedateien für main.cpp, Array.cpp, Array.h, IntArray.cpp, IntArray.h und Makefile. Das Programm ist noch nicht abgeschlossen. Ich habe derzeit nur eine Methode, die die Größe des Arrays erhält.So rufen Sie den Konstruktor einer Vorlagen-Superklasse aus dem Konstruktor einer Vorlagen-Basisklasse in C++ auf?
Fehlermeldung vom 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
Auch wenn Sie Ihre Syntaxfehler lösen verwalten. Ihr Programm wird immer noch nicht funktionieren. Haben Sie gesehen, [warum Vorlagen nur in Header-Datei implementiert werden können] (http://stackoverflow.com/questions/495021/why-can-templates-only-be-implementiert-in-the-header-file)? – WhiZTiM
Verwenden Sie auch nicht die veraltete throught-Spezifikation. Sie sind aus einem bestimmten Grund veraltet. – Rakete1111