C++命名空间中的内联函数
Inline function in namespaces in C++
我正在编写一个矩阵库。我把我的类放在命名空间 SLMath 中。但是由于内联函数,我遇到了错误。
这些是我的文件..
垫4.hpp
#ifndef INC_SLMATH_MAT4_H
#define INC_SLMATH_MAT4_H
#include<cstdint>
#include<algorithm>
namespace SLMath
{
class Mat4
{
typedef std::uint8_t uint8; // You know that Its tedious to write std::uint8_t everytime
float matrix[16];
inline int index(uint8 row,uint8 col) const;
public:
//Constructors
Mat4();
Mat4(const Mat4 &other);
//Destructor
~Mat4();
//operators
void operator=(const Mat4 &other);
//loads the identity matrix
void reset();
//returns the element in the given index
inline float operator()(uint8 row,uint8 col) const;
//returns the matrix array
inline const float* const valuePtr();
};
}
#endif
和垫子4.cpp.。
#include"Mat4.hpp"
namespace SLMath
{
//private member functions
inline int Mat4::index(uint8 row,uint8 col) const
{
return row*4+col;
}
//Public member functions
Mat4::Mat4()
{
reset();
}
Mat4::Mat4(const Mat4 &other)
{
this->operator=(other);
}
Mat4::~Mat4(){}
inline float Mat4::operator()(uint8 row,uint8 col) const
{
return matrix[index(row,col)];
}
void Mat4::reset()
{
float identity[16] =
{
1.0,0.0,0.0,0.0,
0.0,1.0,0.0,0.0,
0.0,0.0,1.0,0.0,
0.0,0.0,0.0,1.0
};
std::copy(identity,identity+16,this->matrix);
}
void Mat4::operator=(const Mat4 &other)
{
for(uint8 row=0 ; row<4 ; row++)
{
for(uint8 col=0 ; col<4 ; col++)
{
matrix[index(row,col)] = other(row,col);
}
}
}
inline const float* const Mat4::valuePtr()
{
return matrix;
}
}
但是当我这样做时..
SLMath::Mat4 matrix;
const float *const value = matrix.valuePtr();
在主要功能中 它给了我一个链接错误...
Main.obj : error LNK2019: unresolved external symbol "public: float const * __thiscall SLMath::Mat4::valuePtr(void)" (?valuePtr@Mat4@SLMath@@QAEQBMXZ) referenced in function _main
和当我从函数 valuePtr() 中删除内联关键字时。它工作正常。请帮帮我...这里还有一件事不清楚,如果编译器为函数 valuePtr() 给出错误,那么它也应该给出运算符()(uint8,uint8) 的错误,因为它声明了内联?
内联函数应在使用它的每个 TU 中定义。这意味着您不能在标头中放置声明并在实现文件中定义函数。
7.1.2/4
内联函数应在使用它的每个翻译单元中定义,并且在每种情况下都应具有完全相同的定义。
相关文章:
- 在命名空间中定义函数还是限定函数
- CUDA内核和数学函数的显式命名空间
- 在命名空间中使用全局命名空间中的函数
- 是否可以将函数导入命名空间,但不能导出它?
- 使用另一个命名空间中的函数C++
- Visual Studio 无法解决类和命名空间中重载的明确函数
- 命名空间更改函数定义
- C++命名空间中调用 void 函数的错误
- C++ 17 个友元函数声明和内联命名空间
- MFC 函数来自 :: 命名空间?
- 在命名空间内使用函数的循环包含
- 在命名空间中使用指令和函数重载
- 命名空间中未引用的函数
- 使用类指针重载C++命名空间函数模板专用化替代方法?
- cgo调用C++中定义的函数(位于命名空间中)
- c++ 内联友元函数是否会导致命名空间之间的名称隐藏?
- 在命名空间内部还是外部实现 c++ 函数?
- 如何在命名空间中正确调用此函数?c++
- 在同一函数中使用不同的命名空间
- 如何从嵌套在命名空间中的类调用成员函数?