源文件中的模板化类
Templated class in source file
本文关键字:源文件 更新时间:2023-10-16
从这个问题移动到。cpp文件中存储c++模板函数定义,我试图在头文件和源文件中分离模板化类的代码。然而,我失败了,但我希望有人能对这种情况有所启发。注意,问题的不同之处在于,他有一个模板化的函数,而不是一个类。
file.h
template<typename T>
class A {
public:
A();
private:
T a;
};
file.cpp
#include "file.h"
template<typename T>
A::A() { a = 0; }
template<int> class A;
和main.cpp
#include "file.h"
int main() {
A<int> obj;
return 0;
}
和错误:
../file.cpp:4:1: error: invalid use of template-name ‘A’ without an argument list A::A() { a = 0; }
^ In file included from ../file.cpp:1:0: ../file.h:1:10: error: template parameter ‘class T’ template<typename T>
^ ../file.cpp:6:21: error: redeclared here as ‘int <anonymous>’ template<int> class A;
^ make: *** [file.o] Error 1
像这样修改你的。cpp文件:
template<typename T>
A<T>::A() { a = 0; } // note the <T>
template class A<int>;
相关文章:
- 为测试目标创建具有不同源文件夹的文件
- 将图像添加到资源文件夹UWP C++
- 视觉工作室项目.提取源文件夹名称
- 生成一个生成文件,该生成文件使用Automake在一个步骤中编译和链接所有源文件
- make 命令如何避免重新编译未更改的源文件?
- C++两个源文件之间共享的枚举的静态实例
- 如何使用Qt 3D库加载和显示搅拌机.obj源文件场景
- 生成文件:动态源文件名和对象目录
- 无法使用 CMake 从其他文件夹添加源文件
- MinGW64 - Unicode 源文件错误?
- 有关从C++中的不同源文件引用类的问题
- "错误 (活动) E1696 无法打开源文件 "stdafx.h"--
- CMakeLists:使用 ExternalProject 从 Github 添加源文件
- 如何打印源文件中与 GDB 中的程序流关联的所有行号
- CMake 源文件找不到头文件
- 使用宏计算源文件行?
- 如何在 Eclipse 中一次从多个源文件进行构建
- 使用多个源文件时对类成员函数的未定义引用
- 给定一个源文件,有没有办法要求 gcc 返回仅直接包含的头文件的列表?
- 是否可以跨多个源文件构建 constexpr 数据结构?