我遇到了一个奇怪的问题,即尝试内联
我的“Person”类的访问器导致代码无法编译。
以下代码将编译并运行成功(使用 Visual Studio 2012):
Person.h
#pragma once
#include <string>
using namespace std;
class Person
{
public:
Person(string name, int age = 0);
~Person(void);
// Accessors
string name(void) const;
int age (void) const;
private:
string m_name;
int m_age;
};
Person.cpp
#include "stdafx.h"
#include "Person.h"
Person::Person(string name, int age) :
m_name(name),
m_age (age )
{}
Person::~Person(void) {}
string Person::name(void) const
{
return m_name;
}
int Person::age(void) const
{
return m_age;
}
header_test.cpp
#include "stdafx.h"
#include <iostream>
#include "Person.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
Person p("Joe");
cout << p.name() << endl;
return 0;
}
如果我将访问器更改为定义为 内联
代码中断的函数。强>
在 Person.h 中内联访问器
// Accessors
inline string name(void) const;
inline int age (void) const;
在 Person.cpp 中内联访问器
inline string Person::name(void) const
{
return m_name;
}
inline int Person::age(void) const
{
return m_age;
}
这样做会产生以下错误:
1>header_test.obj : error LNK2019: unresolved external symbol "public: class std::basic_string,class std::allocator > __thiscall Person::name(void)const " (?name@Person@@QBE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) referenced in function _wmain
1>fatal error LNK1120: 1 unresolved externals
天哪,错误消息是神秘的...感谢您提供所有这些非常有用的信息 Microsoft/Visual Studio!
我知道 inline
关键字只是对编译器的“提示”,在这里可能没有实际值(value),但它仍然不应该破坏代码!
为什么会这样?
最佳答案
我不是语言律师,所以我无法判断编译器行为是否合法。然而,我可以解释发生了什么。
当您将函数标记为inline
时,您不是暗示编译器它可以内联该函数。由于 10 多年以来,编译器在这里不需要您的提示。他们知道什么时候内联。相反,您所做的是指示函数定义对于它包含在其中的每个翻译单元都是本地的。对于此定义应该可用。
实际上您所说的是 name()
定义对于每个 .cpp
文件都应该是本地的,但是您并没有让它对每个 都可用。 cpp
文件!我仍然相信编译器会在这里发出警告。
关于c++ - 为什么内联我的访问器会破坏我的代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33921619/