`
omygege
  • 浏览: 1386652 次
文章分类
社区版块
存档分类
最新评论

“undefined reference to”c++ template

 
阅读更多

You need to use the export keyword. However, I don't think G++ has proper support, so you need to include the template function's definition in the header so the translation unit can use it. This is because the <int> 'version' of the template hasn't been created, only the <typename T> 'version.'

An easy way is to #include the .cpp file. However, this can cause problems, e.g. when other functions are in the .cpp file. It will also likely increase the compile time.

A clean way is to move your template functions into its own .cpp file, and include that in the header or use the export keyword and compile it separately.

c++中template不支持声明和实现分别放在不同的文件中。可以通过include cpp文件解决这个问题。

分享到:
评论

相关推荐

    C++错误代码详细解释

    - **错误示例:** "undefined reference to `function_name'" 链接器找不到你在代码中引用的函数或变量的定义。这可能是由于忘记包含相应的源文件,或者函数没有正确声明和实现。 4. **运行时错误** (Runtime ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    This can be used to keep your template definitions easy to read. Do not forget that a -inl.h file requires a #define guard just like any other header file. Function Parameter Ordering link ▶When...

    c++出错提示英汉对照表

    69. **Undefined reference to 'xxx'** - 对'xxx'的未定义引用:链接时找不到符号。确保所有使用的库和符号都被正确链接。 70. **Unexpected end of file in translation unit** - 翻译单元中意外的文件末尾:...

    acpi控制笔记本风扇转速

    Added #pragma pack(8/4) to acobject.h to ensure that the structures in this header are always compiled as aligned. The ACPI_OPERAND_OBJECT has been manually optimized to be aligned and will not work ...

    libstd.so文件

    - 如果遇到`undefined reference`错误,可能是因为缺少对libstdc++库的链接。 5. **调试与优化** - 使用gdb调试工具可以跟踪libstdc++库中的函数调用。 - `-O2`或更高优化级别可以提高程序性能,但可能影响调试...

    linux-g++-otl-连接oracle-数据库简单例子.doc

    类似地,链接时可能遇到“undefined reference”错误,这通常是因为缺少Oracle的客户端库,可以通过`-L`选项添加库文件路径,如`-L/oradb/ora10g/oracle/product/10.2.0/db_1/lib/`,并使用`-lclntsh`链接到Oracle...

    linux-g++-otl-连接oracle-数据库简单例子.docx

    - `undefined reference`错误通常意味着链接阶段缺少所需的库。在`makefile`中添加`-L/oradb/ora10g/oracle/product/10.2.0/db_1/lib/`指定库路径,并且使用`-lclntsh`链接Oracle客户端库。 4. **使用OTL进行...

    QT x86和arm平台 (2).pdf

    如果遇到`undefined reference to `operator new[](unsigned)'`等链接错误,需要修改`tmake.conf`文件,将`TMAKE_LINK`和`TMAKE_LINK_SHLIB`改为`arm-linux-g++`。 ```bash gedit $TMAKEPATH/tmake.conf ``` 确保...

Global site tag (gtag.js) - Google Analytics