当我们在该类的头文件中定义类成员函数时,必须使用inline关键字.为什么? [英] when we define a class member function in header file of that class then inline keyword must be used. why?

查看:293
本文介绍了当我们在该类的头文件中定义类成员函数时,必须使用inline关键字.为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在头文件中定义了一个类,并在同一头文件中实现了它的功能. 但是在定义这些功能时,我必须将内联关键字与功能定义放在一起.否则,编译器会给出编译时错误.

我知道内联只是对编译器的提示.那么,为什么必须在函数定义中放入内联关键字.

我正在使用带有qt的Visual Studio编译器来编译代码

这是代码

tempinline.h

#ifndef TEMPINLINE_H
#define TEMPINLINE_H
#include "iostream"
class tempinline
{
public:
    tempinline();
    void printH();
};
void tempinline::printH()
{
    std::cout << "hhhh";
}

#endif // TEMPINLINE_H

tempinline.cpp

#include "tempinline.h"

tempinline::tempinline()
{
}

main.cpp

#include <iostream>
#include "tempinline.h"

using namespace std;

int main()
{
    tempinline aa;
            aa.printH();
    cout << "Hello World!" << endl;
    return 0;
}

错误

OUT:debug\tempinline.exe @C:\Users\utrade\AppData\Local\Temp\8\tempinline.exe.8256.687.jom
LINK : debug\tempinline.exe not found or not built by the last incremental link; performing full link
tempinline.obj : error LNK2005: "public: void __thiscall tempinline::printH(void)" (?printH@tempinline@@QAEXXZ) already defined in main.obj
debug\tempinline.exe : fatal error LNK1169: one or more multiply defined symbols found
jom: C:\Users\utrade\build-tempinline-Desktop-Debug\Makefile.Debug [debug\tempinline.exe] Error 1169
jom: C:\Users\utrade\build-tempinline-Desktop-Debug\Makefile [debug] Error 2
18:36:20: The process "C:\Qt\qtcreator-3.0.0\bin\jom.exe" exited with code 2.
Error while building/deploying project tempinline (kit: Desktop)
When executing step 'Make'

解决方案

在进行了很多尝试之后,我才能够编译我的代码 我在 tempinline .cpp 中注释了该代码,还注释了 构造函数timeinline();声明. 因此,这里发生的是,当我在项目中包含头文件的次数多于一次,然后编译器看到void tempinline::printH()函数的多个定义时.因此编译器无法知道要链接的函数,并且给出了链接器错误.

但是,如果我们将内联关键字指定为inline void tempinline::printH()函数,则由于内联关键字的行为,编译器不必链接此函数,因为该函数中的代码会将其替换(内联属性)到任何地方被称为

i defined a class in header file and implemented its function in same header file. but while defining these functions i have to put inline keyword with function definition. Otherwise compiler gave compile time error.

I know inline is only a hint to compiler. So why it is necessary to put inline keyword with function definition.

I am using visual studio compiler with qt for compiling the code

here is the code

tempinline.h

#ifndef TEMPINLINE_H
#define TEMPINLINE_H
#include "iostream"
class tempinline
{
public:
    tempinline();
    void printH();
};
void tempinline::printH()
{
    std::cout << "hhhh";
}

#endif // TEMPINLINE_H

tempinline.cpp

#include "tempinline.h"

tempinline::tempinline()
{
}

main.cpp

#include <iostream>
#include "tempinline.h"

using namespace std;

int main()
{
    tempinline aa;
            aa.printH();
    cout << "Hello World!" << endl;
    return 0;
}

error

OUT:debug\tempinline.exe @C:\Users\utrade\AppData\Local\Temp\8\tempinline.exe.8256.687.jom
LINK : debug\tempinline.exe not found or not built by the last incremental link; performing full link
tempinline.obj : error LNK2005: "public: void __thiscall tempinline::printH(void)" (?printH@tempinline@@QAEXXZ) already defined in main.obj
debug\tempinline.exe : fatal error LNK1169: one or more multiply defined symbols found
jom: C:\Users\utrade\build-tempinline-Desktop-Debug\Makefile.Debug [debug\tempinline.exe] Error 1169
jom: C:\Users\utrade\build-tempinline-Desktop-Debug\Makefile [debug] Error 2
18:36:20: The process "C:\Qt\qtcreator-3.0.0\bin\jom.exe" exited with code 2.
Error while building/deploying project tempinline (kit: Desktop)
When executing step 'Make'

解决方案

after doing a lot of trying i am able to compile my code i comment the code in tempinline .cpp and also comment constructor timeinline(); declaration. So what was happening here is when i am including the header file more then once in a project then compiler see the multiple definitions of void tempinline::printH() function. so compiler was not able to know which function to linked and was giving linker error.

But if we specify inline keyword with function that is inline void tempinline::printH() then because of behavior of inline keyword, compiler do not have to link this function due to replacement(inline property) of code that is in the function to whereever it will be called

这篇关于当我们在该类的头文件中定义类成员函数时,必须使用inline关键字.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆