0

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'
  • 1
    At a *guess*, you're talking about [tag:c++], but I shouldn't *have* to guess. Please [edit] your question and add an appropriate language tag. – Damien_The_Unbeliever Jun 12 '15 at 12:27
  • It's usually not necessary to state `inline`. Show some code, please. – AndyG Jun 12 '15 at 12:35
  • See [This answer](http://stackoverflow.com/questions/10535667/does-it-make-any-sense-to-use-inline-keyword-with-templates/10536588#10536588) for the one scenario when `inline` is required. "For non fully specialized function templates, i.e. ones that carry at least one unknown type, you can omit inline, and not receive errors, but still they are not inline. For full specializations, i.e. ones that use only known types, you cannot omit it." – AndyG Jun 12 '15 at 12:39
  • 1
    possible duplicate of [Why are C++ inline functions in the header?](http://stackoverflow.com/questions/5057021/why-are-c-inline-functions-in-the-header) – Emil Laine Jun 12 '15 at 12:41
  • just check the example that i have posted just now – Himanshu Mahar Jun 12 '15 at 13:06
  • Unable to reproduce. This code compiled and ran without keyword 'inline' and without error on my Ubuntu 15.04. (no qt) – 2785528 Jun 12 '15 at 15:33
  • yes on gcc compiler it is running fine but iam compiling this by visual studio compiler in qt ceater – Himanshu Mahar Jun 12 '15 at 19:24

1 Answers1

0

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

  • Actually, with modern C++, the compiler will try to resolve multiple redefinitions of an "inline" function into a single, shared call target. On the other hand, multiple definitions of a "static" function will each be separately compiled into each translation unit in which it is used. – Christopher Oicles Jun 13 '15 at 01:34
  • What about the functions that are niether static nor inline – Himanshu Mahar Jun 13 '15 at 14:35