【发布时间】:2020-04-07 23:17:25
【问题描述】:
我正在学习 C++,目前正在测试内联函数。如果我现在运行我的代码,我将遇到链接错误,但如果我更改inline void Test::print40()
到
void Test::print40()
一切都会好的。您能否向我解释一下为什么我会出错以及在这种情况下如何使用内联函数。
// main.cpp file
#include "Test.h"
using namespace std;
int main()
{
Test obj1;
obj1.print40();
}
// Test.cpp file
#include <iostream>
#include "Test.h"
inline void Test::print40()
{
std::cout << "40";
}
// Test.h file
#pragma once
class Test
{
public:
void print40();
};
【问题讨论】:
标签: c++ function class inline-functions