0

I've got: frw_trafic.h:

#define PI 3.14159265

namespace FRW
{
float angleToProperRadians(float angle);

class Car 
     {
public:
...
void transform(int time = 1);
...
private:
...
float velocDir; // angle in degrees, same as Sector angle
float wheelDir; // hence delta angle between car velocity direction and where wheels drive direction
float centerPosX;
float centerPosY; //these two are for car position
... }; }

Here is a namespace with a class and a method declared. frw_traffic.cpp

#ifndef FRWTRAFFIC
#define FRWTRAFFIC
#include "frw_traffic.h"
#endif

#include <cmath>

using namespace FRW;

 float angleToProperRadians(float angle)
 {
for (; angle < -360 || angle > 360;)
{
    if (angle < -360)
    {
        angle = angle + 360;
        continue;
    }
    if (angle > 360)
    {
        angle = angle - 360;
    }
}

if (angle < 0)
{
    angle = 360 - angle;
}

return angle * PI / 180;
}
void Car::transform(int time) {
if (wheelDir == 0)
{
    centerPosX = centerPosX + static_cast<float>(time) * speed * cos(FRW::angleToProperRadians(velocDir)) ;
    centerPosY = centerPosY + static_cast<float>(time) * speed * sin(FRW::angleToProperRadians(velocDir)) ;
} }

Method angleToProperRadians() is declared in .h, defined in .cpp and uses for calculations a macro PI, which is defined in .h. Then, I calculate objects position in a line-trajectory with method Car::tranform(). It is also declared in an .h file as a part of a Car class, and defined in .cpp file.

This code fails to compile, giving the "Unresolved external symbol." error. AFA this is a linkage error, I believe something is messed up with macros or includes. I've been desperately trying to get use of other questions about this error here, on Stack Overflow, however most of the people meet this error whilst using external libraries.

Please, somebody, provide an advice on what to check twice to see what's really wrong with this code.

error LNK2019: unresolved external symbol "float __cdecl FRW::angleToProperRadians(float)" (?angleToProperRadians@FRW@@YAMM@Z) referenced in function "public: void __thiscall FRW::Car::transform(int)" (?transform@Car@FRW@@QAEXH@Z)

Thank you.

  • angleToProperRadians isn't defined inside the namespace. –  Nov 05 '16 at 11:31
  • Yves Daoust, you could've actually type this as a reply. Because I did put FRW:: before definition in .cpp. Guess what? It worked! Could you, please, rewrite it as an answer to receive points? –  Nov 05 '16 at 11:46

1 Answers1

0

Actually, in this particular case macros are nowhere to blame. Linkage was done wrong because angleToProperRadians() method declared in the .h and defined in the .cpp files are COMPLETELY separate functions.

In fact, one from .h is now in the FRW namespace. In fact, one from .cpp is now an another, new global function. And "using namespace FRW;" doesn't actually help the situation because it is usable for referring to an already defined method.

This was a proper way in .cpp:

...
float FRW::angleToProperRadians(float angle)
{
for (...) {
...

Notice, how it is 180-turn when we are first referring to a class and then defining its method.

That's it. Thanks for the Yves Daoust for pointing this out in a comment.