I write the following three files: numeros.h, numeros.cpp and main.cpp.
I want to define the inline function outside the body class, in numeros.cpp. Acording to https://isocpp.org/wiki/faq/inline-functions#where-to-put-inline-keyword It's ok to put the inline keyword before the definition. The strange thing is that if I delete inline keyword the program compiles.
//numeros.h
#ifndef NUMEROS_H
#define NUMEROS_H
class Numeros
{
public:
Numeros();
int valor();
private:
int x;
};
#endif
-
//numeros.cpp
#include "numeros.h"
Numeros::Numeros()
{
x = 10;
}
inline int Numeros::valor()
{
return x;
}
-
//main.cpp
#include "numeros.h"
#include <iostream>
using namespace std;
int main()
{
Numeros n1;
cout<< n1.valor();
cin.get();
return 0;
}
EDIT:
Build error on Visual Studio 2015:
1>main.obj : error LNK2019: símbolo externo "public: int __thiscall Numeros::valor(void)" (?valor@Numeros@@QAEHXZ) sin resolver al que se hace referencia en la función _main 1>c:\users\pablo\documents\visual studio 2015\Projects\Project6\Debug\Project6.exe : fatal error LNK1120: 1 externos sin resolver
Build error on Qt Creator 3.5.1:
C:\Users\Pablo\Dropbox\QtProjects\Windows\build-untitled-Desktop_Qt_5_5_1_MinGW_32bit-Debug\debug\main.o:-1: In function `main':
C:\Users\Pablo\Dropbox\QtProjects\Windows\untitled\main.cpp:13: error: undefined reference to `Numeros::valor()'
collect2.exe:-1: error: error: ld returned 1 exit status