gpt4 book ai didi

c++ - 单文件程序的未定义虚表

转载 作者:行者123 更新时间:2023-11-28 00:26:16 25 4
gpt4 key购买 nike

<分区>

我编写了以下仿函数的最小“沙盒”代码。

#include<cstdio>

struct Functor {
virtual void operator() ();
virtual ~Functor() {}
};

template<class Class, class FPtr>
struct FunctorImpl : Functor {
FunctorImpl( Class const *_obj, FPtr _fptr ) : obj(_obj), fptr(_fptr) {}
virtual void operator() () { (obj->*fptr)(); }
virtual ~FunctorImpl() {}
protected:
Class const *obj;
FPtr fptr;
};

template<class Class, class FPtr>
FunctorImpl<Class,FPtr> *make_Functor( Class const *obj, FPtr fptr ) {
return new FunctorImpl<Class,FPtr>(obj,fptr);
}

struct FOO {
void bar() const { printf("1\n"); }
void bar2() const { printf("2\n"); }
};

int main()
{
FOO f;
Functor* f1 = make_Functor( &f, &FOO::bar );
Functor* f2 = make_Functor( &f, &FOO::bar2 );
(*f1)();
(*f2)();
}

但是在编译时出现错误(使用 gcc (GCC) 4.8.3 20140624 (Red Hat 4.8.3-1))

/tmp/cc4ayCAU.o: In function `Functor::~Functor()':
functor.cpp:(.text._ZN7FunctorD2Ev[_ZN7FunctorD5Ev]+0x13): undefined reference to `vtable for Functor'
/tmp/cc4ayCAU.o: In function `Functor::Functor()':
functor.cpp:(.text._ZN7FunctorC2Ev[_ZN7FunctorC5Ev]+0xf): undefined reference to `vtable for Functor'
/tmp/cc4ayCAU.o:(.rodata._ZTI11FunctorImplI3FOOMS0_KFvvEE[_ZTI11FunctorImplI3FOOMS0_KFvvEE]+0x10): undefined reference to `typeinfo for Functor'

这怎么可能只编译一个文件?这个问题的解决方案是什么?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com