[英]Inline function in namespaces in C++
我正在寫一個矩陣庫。 我將我的類放在命名空間 SLMath 中。 但由於內聯函數,我遇到了錯誤。
這些是我的文件..
Mat4.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
和 Mat4.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() 中刪除 inline 關鍵字時..它工作正常。 請幫幫我...這里還有一件不清楚的事情是,如果編譯器為函數 valuePtr() 給出錯誤,那么它也應該為 operator()(uint8,uint8) 正確給出錯誤,因為它聲明為內聯?
內聯函數應在使用它的每個 TU 中定義。 這意味着您不能在頭文件中放置聲明並在實現文件中定義函數。
7.1.2/4
內聯函數應在每個使用 odr 的翻譯單元中定義,並且在每種情況下都應具有完全相同的定義。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.