模板类的成员实现的未定义引用

发布于 2024-09-13 02:22:46 字数 1091 浏览 2 评论 0原文

这对我来说完全是神秘的。我在 ubuntu 上使用 g++,这是我的一些代码(类名发生了变化,但没有别的,因为我仍然在各处使用存根):

Bob.hpp

template <class A>
class Bob : public Jack<Chris, A>
{
    public: 

        Bob(int x1, int x2, float x3 = 1.0, float x4 = 2.0, float x5 = 3.0) throw(Exception);
        virtual ~Bob();
};

我在另一个文件中实现,例如这个:

Bob.cpp

template <class A>
Bob<A>::Bob(int x1, int x2, float x3, float x4, float x5) throw(Exception)
{

}

template <class A>
Bob<A>::~Bob()
{

}

,我这样使用它:

ma​​in.cpp

int main()
{
    Bob<Alice> instance(1, 2);
}

编译为:

g++ -c Bob.cpp -o Bob.o
g++ -c main.cpp -o main.o
g++ -L"libs" -llib main.o Bob.o prog

给我 main.o:在函数 main' 中: main.cpp:(.text+0x1fd): 对 Bob::Bob(int, int, float, float, float)' 的未定义引用 Collect2: ld 返回 1 退出状态

我完全被难住了。使用 g++ 链接阶段更改顺序没有什么区别。编译目标文件不会产生任何问题。为什么当我实现构造函数时出现未定义的引用?如果有人能阐明这一点,我们将不胜感激。

This is wholy mysterious to me. I'm using g++ on ubuntu, and this is some of my code (with class names change, but nothing else because I'm still using stubs everywhere):

Bob.hpp

template <class A>
class Bob : public Jack<Chris, A>
{
    public: 

        Bob(int x1, int x2, float x3 = 1.0, float x4 = 2.0, float x5 = 3.0) throw(Exception);
        virtual ~Bob();
};

I implemented in another file like this:

Bob.cpp

template <class A>
Bob<A>::Bob(int x1, int x2, float x3, float x4, float x5) throw(Exception)
{

}

template <class A>
Bob<A>::~Bob()
{

}

and I used it like this:

main.cpp

int main()
{
    Bob<Alice> instance(1, 2);
}

Compiling with:

g++ -c Bob.cpp -o Bob.o
g++ -c main.cpp -o main.o
g++ -L"libs" -llib main.o Bob.o prog

gives me
main.o: In function main':
main.cpp:(.text+0x1fd): undefined reference to
Bob::Bob(int, int, float, float, float)'
collect2: ld returned 1 exit status

I am completely stumped. Changing the order with the g++ linking stage makes no difference. Compiling the object files generates no problems. And Why an undefined reference when I implemented the constructor? If anyone could shed any light on this, it's be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

萧瑟寒风 2024-09-20 02:22:46

您需要将代码从 Bob.cpp 移至 Bob.hpp。当编译器在 Bob.cpp 中看到 Bob::BobBob::~Bob 的定义时,它不知道实际要实例化什么类型的 Bob (即 BobBob 并且不会生成它们的代码。
或者,您仍然可以将代码放在 Bob.cpp 文件中,但您需要声明要实例化哪种类型的 Bob,例如:
Bob.cpp 内部:

template
class Bob<Alice>;

You need to move the code from Bob.cpp into Bob.hpp. When the compiler sees the definitions of Bob::Bob and Bob::~Bob in Bob.cpp, it does not know what types of Bob are actually going to be instantiated (i.e. Bob<int> vs Bob<SomeClass> and the code for them isn't generated.
Alternatively, you can still place the code in the Bob.cpp file, but you need to declare which types of Bob are going to be instantiated, e.g.:
Inside of Bob.cpp:

template
class Bob<Alice>;

类模板成员函数的声明和定义应该都在同一个头文件中。

编译 Bob.cpp 时,编译器具有可用的声明和定义。此时,编译器不需要为模板类生成任何定义,因为没有实例化。当编译器编译main.cpp时,有一个实例化:模板类Bob。此时编译器有声明但没有定义!

The declarations and definitions of the class template member functions should all be in the same header file.

When compiling Bob.cpp, the compiler has both the declarations and the definitions available. At this point the compiler does not need to generate any definitions for template classes, since there are no instantiations. When the compiler compiles main.cpp, there is an instantiation: template class Bob<Alice>. At this point the compiler has the declarations but no definitions!

征棹 2024-09-20 02:22:46

除了其他人提出的问题之外,库必须位于 GCC 命令行的最后。而不是:

g++ -L"libs" -llib main.o Bob.o prog

你想要:

g++ -L"libs"  main.o Bob.o prog -llib

In addition to the issues raised by others, libraries must come last on the GCC command line. Instead of:

g++ -L"libs" -llib main.o Bob.o prog

you want:

g++ -L"libs"  main.o Bob.o prog -llib
中二柚 2024-09-20 02:22:46

您认为 Bob 的构造函数应该定义在哪里?它没有在 Bob.cpp 中定义,因为 Bob.cpp 中没有提及 Bob。当 Bob.cpp 编译成 Bob.o 时,有一个模板可以用来定义 Bob,但事实并非如此。

将模板定义放入 Bob.hpp 中,或将 Bob 放入 Bob.cpp 中。

Where do you think the constructor of Bob<Alice> should be defined? It wasn't defined in Bob.cpp, because there was no mention of a Bob<Alice> in Bob.cpp. There was a template, which could have been used to define Bob<Alice> when Bob.cpp was compiled into Bob.o, but it wasn't.

Put the template definition in Bob.hpp, or Bob<Alice> in Bob.cpp.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文