【发布时间】:2019-12-16 04:55:05
【问题描述】:
我正在学习 C++ 并使用 CLion 来构建项目。我在main 目录中有main.cpp 文件。我还有一个main/Objects 目录,里面有Vec3.h 和Vec3.cpp 文件:
//Vec3.h
template<typename T>
class Vec3
{
public:
// 3 most basic ways of initializing a vector
Vec3() : x(T(0)), y(T(0)), z(T(0)) {}
Vec3(const T &xx) : x(xx), y(xx), z(xx) {}
Vec3(T xx, T yy, T zz) : x(xx), y(yy), z(zz) {}
T x, y, z;
T length();
};
.
//Vec3.cpp
#include "Vec3.h"
#include <stdio.h>
#include <math.h>
template <typename T>
T Vec3<T>::length()
{
return sqrt(x * x + y * y + z * z);
}
.
//main.cpp
#include <iostream>
#include "Objects/Vec3.h"
int main() {
Vec3<double> vec(3, 4, 0);
std::cout << "Hello, World!" << std::endl;
std::cout << vec.length() << std::endl;
return 0;
}
.
//CMakeLists.txt
cmake_minimum_required(VERSION 3.13)
project(Testing)
set(CMAKE_CXX_STANDARD 14)
set(SOURCE_FILES main.cpp Objects/Vec3.cpp)
add_executable(Testing ${SOURCE_FILES})
当我尝试构建和运行时,我收到以下错误:
undefined reference to "Vec3<double>::length()". ld returned 1 exit status
我没有向链接器提供什么信息?
【问题讨论】:
标签: c++ cmake linker undefined-reference