Fórumok
Sziasztok!
Ezt a példa-programot írtam:
main.cpp :
#include "tempclass.h"
#include "nontempclass.h"
int main() {
TempClass <int> tc;
tc.printSomething(34);
NonTempClass ntc;
ntc.printSomething(34);
return 0;
}
tempclass.h :
#ifndef TEMPCLASS_H
#define TEMPCLASS_H
template <class T> class TempClass {
public:
TempClass();
void printSomething(T input);
};
#endif
tempclass.cpp :
#include <iostream>
#include "tempclass.h"
template <class T> TempClass<T>::TempClass() {
}
template <class T> void TempClass<T>::printSomething(T input) {
std::cout << "\nTemplate is printing this: " << input << "\n\n";
}
nontempclass.h :
#ifndef NONTEMPCLASS_H
#define NONTEMPCLASS_H
class NonTempClass {
public:
NonTempClass();
void printSomething(int input);
};
#endif
nontempclass.cpp :
#include <iostream>
#include "nontempclass.h"
NonTempClass::NonTempClass() {
}
void NonTempClass::printSomething(int input) {
std::cout << "\nNon template is printing this: " << input << "\n\n";
}
A program fordításakor a következő hibát kapom a g++ fordítótól:
main.o: In function `main':
main.cpp:(.text+0x1d): undefined reference to `TempClass::TempClass()'
main.cpp:(.text+0x30): undefined reference to `TempClass::printSomething(int)'
Miért ez a különbség a sablon osztály és nem sablon osztály között? Miért nem tudja lefordítani a sablon osztályt rendesen?
- 939 megtekintés
Hozzászólások
- A hozzászóláshoz be kell jelentkezni
Kösz "zleho"!
Ennyi információ elég is.
- A hozzászóláshoz be kell jelentkezni