包括 .cpp 文件? [英] Include .cpp file?

查看:17
本文介绍了包括 .cpp 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
为什么模板只能在头文件中实现?

我最近一直在尝试使用 C++.目前我正在尝试编写一些我确信每个人都至少做过一次的事情:一个简单的 LinkedList 类.代码已完成,但我不知何故无法编译它.我一直在谷歌搜索,似乎我将目标文件链接错了.我的代码基本上是这样的:

I've been trying around with C++ recently. At the moment I'm trying to program something I'm sure everone has done at least once: A simple LinkedList class. The code is done, but I'm somehow failing to compile it. I've been googling and it seems like I'm linking the object files wrong. That's what my code basically looks like:

test.cpp

#include "linkedlist.h"

int main()
{
    LinkedList<int> list;
    // do something
}

链表.h

template <typename T>
class LinkedList
{
   // a lot of function and variable definitions
}

然后有一个名为linkedlist.cpp 的.cpp 文件,其中包含LinkerList 类的所有实际代码.尝试使用以下命令编译 test.cpp 时:

Then there's a .cpp file called linkedlist.cpp which contains all the actual code of the LinkerList class. When trying to compile test.cpp using the following command:

g++ ..src	est.cpp

我被告知存在对LinkedList::LinkedList()"的未定义引用.所以我一直认为它链接错误,因为有多个 .cpp 文件,所以我这样尝试:

I'm getting told that there's an undefined reference to 'LinkedList::LinkedList()'. So I've been thinking that it's being linked wrong as there's more than one .cpp file, so I tried it like this:

g++ -c -Wall -O2 ..src	est.cpp
g++ -c -Wall -O2 ..srclinkedlist.cpp
g++ -s test.o linkedlist.o

但是,这不会改变任何事情.错误消息保持不变.我一直在尝试在互联网上找到一些信息,但是并没有真正奏效.

However, this doesn't change anything. The error messages stay the same. I've been trying to find some information on the internet, however, it didn't really work out.

推荐答案

你正在创建一个类模板,它有一个重要的警告,即所有变量定义必须放在头文件中,以便所有翻译都可以访问它们编译期间的单位.

You're creating a class template, which has the important caveat that all variable definitions must be placed in the header file so that they're accessible to all translation units during compilation.

原因在于模板的工作方式.在编译期间,编译器会根据您的类模板定义实例化模板类.仅访问声明或签名是不够的:它需要提供完整的定义.

The reason is the way that templates work. During compilation, the compiler instantiates template classes based on your class template definition. It isn't enough for it to have access to only the declarations or signatures: it needs to have the entire definition available.

将所有方法定义从 .cpp 文件中移到 .h 文件中,一切都应该没问题(假设你有,事实上,提供了默认构造函数的定义!).

Move all of your method definitions out of the .cpp file and into the .h file, and everything should be fine (assuming that you have, in fact, provided a definition for the default constructor!).

或者,您可以通过明确告诉您的编译器使用 template class LinkedList<int> 之类的东西来实例化适当的模板类来解决这个问题,但这在这种情况下确实没有必要一个简单的案例.与在头文件中包含所有定义相比,这样做的主要优点是它可能会减少代码膨胀并加快编译速度.但这可能根本没有必要,因为编译器在应用适当的优化方面变得更加聪明.

Alternatively, you might be able to get around this by explicitly telling your compiler to instantiate the appropriate template class using something like template class LinkedList<int>, but this really isn't necessary in such a simple case. The primary advantage of this over including all of the definitions in the header file is that it potentially reduces code bloat and speeds up compilation. But it might not be necessary at all, as compilers have gotten a lot smarter at applying appropriate optimizations.

这篇关于包括 .cpp 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆