C++;类表达式参数定义不正确 我正在学习C++,现在遇到了类模板的奇怪问题。 这是我的头文件: #ifndef VECTOR_H #define VECTOR_H #include <iostream> #include <list> using namespace std; template <int n> class Vector { public: list<float> coords; Vector(); Vector(list<float> ncoords); }; template <int n> Vector<n>::Vector() { coords.assign(n, 0.0); } #endif #ifndef向量 #定义向量 #包括 #包括 使用名称空间std; 模板 类向量{ 公众: 列出合作伙伴; 向量(); 向量(列表); }; 模板 向量::向量(){ 坐标分配(n,0.0); } #恩迪夫

C++;类表达式参数定义不正确 我正在学习C++,现在遇到了类模板的奇怪问题。 这是我的头文件: #ifndef VECTOR_H #define VECTOR_H #include <iostream> #include <list> using namespace std; template <int n> class Vector { public: list<float> coords; Vector(); Vector(list<float> ncoords); }; template <int n> Vector<n>::Vector() { coords.assign(n, 0.0); } #endif #ifndef向量 #定义向量 #包括 #包括 使用名称空间std; 模板 类向量{ 公众: 列出合作伙伴; 向量(); 向量(列表); }; 模板 向量::向量(){ 坐标分配(n,0.0); } #恩迪夫,c++,c++11,undefined-reference,C++,C++11,Undefined Reference,这是我的.cpp文件: #include "vector.h" #include <list> using std::ostream; using namespace std; template <int n> Vector<n>::Vector(list<float> ncoords): coords {ncoords}{} #包括“vector.h” #包括 使用std::ostream; 使用名称空间std; 模板 Vector::Ve

这是我的.cpp文件:

#include "vector.h"
#include <list>

using std::ostream;
using namespace std;

template <int n>
Vector<n>::Vector(list<float> ncoords): coords {ncoords}{}
#包括“vector.h”
#包括
使用std::ostream;
使用名称空间std;
模板
Vector::Vector(列出ncoords):坐标{ncoords}{
如果我做
Vector,一切正常

但若我尝试,linker会给出一个错误
vector2{list{}

错误消息

对“Vector::Vector(std::list>)”的未定义引用


问题是-如何解决这个问题?

模板必须在头文件中实现。这是由于链接的工作方式。仔细阅读详尽的答案。下次在询问之前搜索。

您不能在.cpp文件中实现模板,必须将其放在标题中。模板必须在标题中实现。看见