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文件解决这个问题。
分享到:
相关推荐
- **错误示例:** "undefined reference to `function_name'" 链接器找不到你在代码中引用的函数或变量的定义。这可能是由于忘记包含相应的源文件,或者函数没有正确声明和实现。 4. **运行时错误** (Runtime ...
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...
69. **Undefined reference to 'xxx'** - 对'xxx'的未定义引用:链接时找不到符号。确保所有使用的库和符号都被正确链接。 70. **Unexpected end of file in translation unit** - 翻译单元中意外的文件末尾:...
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 ...
- 如果遇到`undefined reference`错误,可能是因为缺少对libstdc++库的链接。 5. **调试与优化** - 使用gdb调试工具可以跟踪libstdc++库中的函数调用。 - `-O2`或更高优化级别可以提高程序性能,但可能影响调试...
类似地,链接时可能遇到“undefined reference”错误,这通常是因为缺少Oracle的客户端库,可以通过`-L`选项添加库文件路径,如`-L/oradb/ora10g/oracle/product/10.2.0/db_1/lib/`,并使用`-lclntsh`链接到Oracle...
- `undefined reference`错误通常意味着链接阶段缺少所需的库。在`makefile`中添加`-L/oradb/ora10g/oracle/product/10.2.0/db_1/lib/`指定库路径,并且使用`-lclntsh`链接Oracle客户端库。 4. **使用OTL进行...
如果遇到`undefined reference to `operator new[](unsigned)'`等链接错误,需要修改`tmake.conf`文件,将`TMAKE_LINK`和`TMAKE_LINK_SHLIB`改为`arm-linux-g++`。 ```bash gedit $TMAKEPATH/tmake.conf ``` 确保...