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

查看:245
本文介绍了包括.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
}

linkedlist.h

linkedlist.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\test.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\test.cpp
g++ -c -Wall -O2 ..\src\linkedlist.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!).

或者,你可以明确告诉你的编译器实例化相应的模板类, code>模板类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天全站免登陆