0
中的.cpp我最初的實現方法是這樣的:如何利用編寫向量的矢量通用打印C++
using namespace std;
...
template <typename T>
void print2dvector(vector<vector<T> > v) {
for(int i = 0; i < v.size(); i++) {
for(int j = 0; j < v[i].size(); j++) {
cout << v[i][j] << " ";
}
cout << endl;
}
}
在.h文件中聲明如下
template <typename T> void print2dvector(std::vector<std::vector<T> > v);
這裏是我如何使用它,
print2dvector<int>(vec_of_vec);
編譯階段通過,但在連接階段失敗。錯誤如下:
Undefined symbols for architecture x86_64:
"void print2dvector<int>(std::__1::vector<std::__1::vector<int, std::__1::allocator<int> >, std::__1::allocator<std::__1::vector<int, std::__1::allocator<int> > > >)", referenced from:
spiral_matrix_ii_Challenge::Execute() in spiral_matrix_ii.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
請讓我知道,如果我錯過任何概念在實現這一點。
是在頭文件或實現文件中的函數? –
[OT]:您可以將向量作爲常量引用傳遞以避免複製。 – Jarod42
@Piotr該函數在cpp文件中實現並在頭文件中定義。 – lordofire