0

I want to define operators for a class I created, I want the operators to be inline I also want the definition be in the .cpp file while the declaration is in the .h file I tried to do this:

Vector3D.h

class Vector3D
{
    ...
};

inline Vector3D operator+(Vector3D lv, const Vector3D& rv);
inline Vector3D operator-(Vector3D lv, const Vector3D& rv);

Vector3D.cpp

...
inline Vector3D operator+(Vector3D lv, const Vector3D& rv)
{
    lv += rv;
    return lv;
}

inline Vector3D operator-(Vector3D lv, const Vector3D& rv)
{
    lv -= rv;
    return lv;
}

In the _tmain function the functions are called:

std::cout << v1 << "+" << v2 << "=" << v1+v2 << std::endl;
std::cout << v1 << "-" << v2 << "=" << v1-v2 << std::endl;

I get the errors:

1>RotateShapes.obj : error LNK2019: unresolved external symbol "class Vector3D __cdecl operator+(class Vector3D,class Vector3D const &)" (??H@YA?AVVector3D@@V0@ABV0@@Z) referenced in function _wmain
1>RotateShapes.obj : error LNK2019: unresolved external symbol "class Vector3D __cdecl operator-(class Vector3D,class Vector3D const &)" (??G@YA?AVVector3D@@V0@ABV0@@Z) referenced in function _wmain

If I remove the inline then everything works fine. If I don't call the functions, then compilation is successful. Is there a way to define an inline function in a different place of where it was declared?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
SIMEL
  • 8,745
  • 28
  • 84
  • 130
  • If you `Vector3D` is a class, then in the .cpp file you need to declare the method body as `Vector3D::operator+`. You also need a return type, I think – Vinz Feb 05 '15 at 16:21
  • @Vinzenz You can also use the non member form. – Eric Fortin Feb 05 '15 at 16:23
  • @Vinzenz, `Vector3D` is a class, but these operators are not members of the class. – SIMEL Feb 05 '15 at 16:24
  • 1
    You need to put the inline functions in the header file, for them to be used in other source files. – Neil Kirk Feb 05 '15 at 16:25
  • @IlyaMelamed Do you really need to have them in cpp file. Common pattern is to include a ".inl" file where you put definition at the end of header file. – Eric Fortin Feb 05 '15 at 16:26
  • Okay sorry then, I always got the unresolved linker errors when I used a mehtod somewhere which was not in the scope of the class because I missed the `CLASSNAME::` in front of the member function or operator – Vinz Feb 05 '15 at 16:26

2 Answers2

2

From the standard §3.2/3:

An inline function shall be defined in every translation unit in which it is odr-used.

If you don't define the functions in the header where they're declared inline, then how else do you get the definition into all compilation units that use them?

Sure, you can define the functions in a separate file and include it in the header. They'll technically be in a separate file, but after preprocessing, they'll be part of the header (and then, part of all compilation units which include the header). If that's not what you want, then you can't declare the functions inline.

eerorika
  • 232,697
  • 12
  • 197
  • 326
0

Nope, there is no way - inline functions have to be defined in the header file. See here: C++ inlining class methods causes undefined reference

Community
  • 1
  • 1
Chris
  • 2,030
  • 1
  • 16
  • 22
  • They don't have to be in a header file - they just have to be visible to the compiler when called. – Neil Kirk Feb 05 '15 at 16:24
  • Is there a way to achieve the result of hiding the implementation of an `inline` function in any other way? – SIMEL Feb 05 '15 at 16:26
  • @IlyaMelamed: No. Inline functions must be defined in every translation unit that uses them, so the definition can't be "hidden". – Mike Seymour Feb 05 '15 at 16:27
  • You can hide them to your eyes(not the compiler) if this is your goal by putting them in another file and including it at the end of your header file. Removes some clutter from the class header. – Eric Fortin Feb 05 '15 at 16:29