c++ -为什么体系结构中没有定义的符号:
C++ - Why Undefined symbols for architecture x86_64:
我是c++的新手,我尝试在c++中学习template
,所以我从Essential c++ 写了一个矩阵模板类。
头文件:
#ifndef __MATRIX__
#define __MATRIX__
#include <iostream>
using namespace std;
template <typename elemType>
class Matrix
{
friend Matrix<elemType> operator + ( const Matrix<elemType>&, const Matrix<elemType>& );
friend Matrix<elemType> operator * ( const Matrix<elemType>&, const Matrix<elemType>& );
public:
Matrix( int row, int column );
Matrix( const Matrix& );
~Matrix() { delete [] _matrix; }
int row() const { return _row; }
int column() const { return _column; }
Matrix& operator = ( const Matrix& );
ostream& print( ostream& ) const;
void operator += ( const Matrix& );
elemType& operator () (int row, int column )
{
return _matrix[row * _row + column];
}
elemType operator () (int row, int column ) const
{
return _matrix[row * _row + column];
}
private:
elemType *_matrix;
int _row;
int _column;
};
#endif
源文件:
#include "Matrix.h"
template <typename elemType>
Matrix<elemType>::Matrix( int row, int column ) :
_row(row), _column(column)
{
int size = _row * _column;
_matrix = new elemType[size];
for (int i = 0; i < size; ++i)
{
_matrix[i] = elemType();
}
}
template <typename elemType>
Matrix<elemType>::Matrix( const Matrix& m )
{
cout << "Matrix( const Matrix& m )" << endl;
_row = m._row;
_column = m._column;
int size = _row * _column;
_matrix = new elemType[size];
for (int i = 0; i < size; ++i)
{
_matrix[i] = m._matrix[i];
}
}
template <typename elemType>
Matrix<elemType>& Matrix<elemType>:: operator = ( const Matrix& m )
{
cout << "operator = " << endl;
if( this != &m )
{
_row = m._row;
_column = m._column;
int size = _row * _column;
delete [] _matrix;
_matrix = new elemType[size];
for (int i = 0; i < size; ++i)
{
_matrix[i] = m._matrix[i];
}
}
return *this;
}
template <typename elemType>
ostream& Matrix<elemType>::print( ostream& os ) const
{
os << "===print the matrix:===" << endl;
for (int i = 0; i < _row; ++i)
{
for (int j = 0; j < _column; ++j)
{
os << _matrix[i * _row + j] << ' ';
}
os << endl;
}
return os;
}
template <typename elemType>
void Matrix<elemType>::operator += ( const Matrix& m )
{
for (int i = 0; i < m.row(); ++i)
{
for (int j = 0; j < m.column(); ++j)
{
_matrix[i * _row + j] += m(i,j);
}
}
}
template <typename elemType>
Matrix<elemType> operator + ( const Matrix<elemType>& a, const Matrix<elemType>& b )
{
Matrix<elemType> res(a);
res += b;
return res;
}
template <typename elemType>
Matrix<elemType> operator * ( const Matrix<elemType>& a, const Matrix<elemType>& b )
{
Matrix<elemType> res( a.row(), b.column() );
for (int i = 0; i < a.row(); ++i)
{
for (int j = 0; j < b.column(); ++j)
{
res(i, j) = 0;
for (int k = 0; k < a.column(); ++k)
{
res(i, j) += a(i, k) * b(k, j);
}
}
}
return res;
}
template <typename elemType>
inline ostream& operator << ( ostream& os, const Matrix<elemType>& m )
{
return m.print(os);
}
int main(int argc, char const *argv[])
{
Matrix<int> ima (4,4);
cout << ima << endl;
Matrix<float> fma (4,4);
cout << fma << endl;
float ar[16] =
{
1., 0., 0., 0., 0., 1., 0., 0.,
0., 0., 1., 0., 0., 0., 0., 1.
};
Matrix<float> fmb(4,4);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
fmb(i,j) = ar[i*4+j];
}
}
fma = fmb;
cout << fma << fmb << endl;
float br[16] = { 1.3, .4, 2.6, 8.2, 6.2, 1.7, 1.3, 8.3,
4.2, 7.4, 2.7, 1.9, 6.3, 8.1, 5.6, 6.6 };
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
fmb(i,j) = br[i*4+j];
}
}
cout << fma << fmb << endl;
fma += fma;
cout<< fma << fmb << endl;
fma = fma + fma;
cout << fma << fmb << endl;
return 0;
}
当我在Sublime中编译时,它显示:
Undefined symbols for architecture x86_64:
"operator+(Matrix<float> const&, Matrix<float> const&)", referenced from:
_main in Matrix-d2d621.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
似乎问题发生在函数operator +
中。
那么,为什么会发生这种情况,我该如何解决?
试着这样开始你的标题
//declare class and friend operators templates
template <typename elemType>
class Matrix;
template <typename elemType>
Matrix<elemType> operator + ( const Matrix<elemType>&, const Matrix<elemType>& );
template <typename elemType>
Matrix<elemType> operator * ( const Matrix<elemType>&, const Matrix<elemType>& );
并像这样在类中声明友元函数
friend Matrix<elemType> operator + <> ( const Matrix<elemType>&, const Matrix<elemType>& );
friend Matrix<elemType> operator * <> ( const Matrix<elemType>&, const Matrix<elemType>& );
类模板中方法的定义应该是
- 定义在header 或
- 定义在for . .impl文件中,包含在声明头或
- 在。cpp文件中显式实例化,在这种情况下,只能使用实例化的版本。
相关文章:
- vscode g++链路故障:体系结构x86_64的未定义符号
- 为函数定义符号不明确的指针参数
- 体系结构x86_64的未定义符号:std:terminate(),typeinfo,运算符delete[],运算符new
- C++ 中 dlsym 的未定义符号
- 仅在 MacOS 上析构函数的未定义符号
- OSX clang++:用于 cpp 文件中显式实例化模板的体系结构x86_64的未定义符号
- PCL 出错:体系结构x86_64 @pcl的未定义符号
- clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用) - 体系结构的未定义符号 x86_64:
- 体系结构x86_64的未定义符号:链接器错误
- C++ std::vector<int> 体系结构的未定义符号 x86_64:
- 生成文件:体系结构x86_64的未定义符号
- libc++abi未定义符号:_ZTVN10__cxxabif120__si-class_type_infoE
- Xcode"体系结构x86_64的未定义符号"
- 体系结构x86_64的未定义符号:找不到原因
- SWIG链接器:未定义符号:_ZN2cv8fastFreeEPv(cv::fastFree(void*))
- 如何修复架构x86_64的未定义符号,Boost Asio 1.58
- 在Qt5上去静态库:架构的未定义符号x86_64:
- 体系结构x86_64的未定义符号:c++/ h 文件和 cpp 文件
- 为什么链接器报告全局函数的乘法定义符号,而不是类静态方法
- 监视 SDK 下的未定义符号"__Unwind_SjLj_Unregister"