我正在尝试编写一个函数来将std :: vectors打印为
template <class T>
void PrintVector(const std::vector<T> vec)
{
for(unsigned int j=0;j<vec.size();j++){ std::cout << vec[j] << "\t"; }
std::cout << "\n";
}
如果我放入项目的主文件,这是有效的。但是当我把它放在一个外部文件(例如functions.cpp和functions.hpp)中作为
文件functions.hpp:
#ifndef functions_hpp
#define functions_hpp
#include <iostream>
#include <vector>
template <class T>
void PrintVector(const std::vector<T> vec);
#endif
文件functions.cpp:
#include "functions.hpp"
template <class T>
void PrintVector(const std::vector<T> vec)
{
for(unsigned int j=0;j<vec.size();j++){ std::cout << vec[j] << "\t"; }
std::cout << "\n";
}
我收到以下编译错误:
Undefined symbols for architecture x86_64:
"void PrintVector<int>(std::__1::vector<int, std::__1::allocator<int> >)", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
functions.cpp中定义的所有其他函数都有效。如何解决这个问题?