我有一些复杂的类。
类 _A 具有子参数化类 A,它有两个子 A1 和 A2。
B类。包含作为成员的类_A对象的指针。有两个子类 B1 和 B2,分别对应于类 A1 和 A2。B1 将 _A 构造为 A1。B2 分别为 A2。
最后是在 B 类内部定义了子 BY 的 Y 类。
现在它是如何出现在文件中的。
tf1.h
#include <iostream>
struct Y{ // goes to the file tf2.h as ancestor of the class B::BY
};
struct _A{ // goes to the file tf2.h as a member of the class B
};
template<class X>
struct A: public _A { // goes to two classes below only as an ancestor
virtual void method();
protected:
virtual void m() = 0;
};
template<class X>
struct A1: public A<X>{ // goes to the file tf2.h to the class B1
protected:
void m();
};
template<class X>
struct A2: public A<X>{ // goes to the file tf2.h to the class B2
protected:
void m();
};
tf1.cpp
#include "tf1.h"
template<class X>
void A<X>::method(){
/* here the class X used */
std::cout << "A::method called" << std::endl;
m();
}
template<class X>
void A1<X>::m(){
std::cout << "A1::m called" << std::endl;
}
template<class X>
void A2<X>::m(){
std::cout << "A1::m called" << std::endl;
}
tf2.h
#include "tf1.h"
class B{ // is the counterpain of the class _A
protected:
class BY: public Y{
};
_A * mp_x;
};
class B1: public B{ // is the counterpain of the class A1
public:
B1(){mp_x = new A1<BY>; std::cout << "B::B is called" << std::endl;}
};
class B2: public B{ // is the counterpain of the class A2
public:
B2(){mp_x = new A2<BY>; std::cout << "C::C is called" << std::endl;}
};
tfmain.cpp
#include <stdlib.h>
#include "tf2.h"
int main (int,char**){
B2 b2;
system ("PAUSE");
}
最后,问题。
d:>g++ -std=c++0x tfmain.cpp -o tfmain.exe
ccB2BnSP.o:tfmain.cpp:(.rdata$_ZTV2A2IN1B2BYEE[__ZTV2A2IN1B2BYEE]+0x8): undefined reference to `A<B::BY>::method()'
ccB2BnSP.o:tfmain.cpp:(.rdata$_ZTV2A2IN1B2BYEE[__ZTV2A2IN1B2BYEE]+0xc): undefined reference to `A2<B::BY>::m()'
ccB2BnSP.o:tfmain.cpp:(.rdata$_ZTV1AIN1B2BYEE[__ZTV1AIN1B2BYEE]+0x8): undefined reference to `A<B::BY>::method()
我正在使用 MinGW v4.7.2