I have these two simple files that define a C++ class with a tryME function
______myclass.h________________
#pragma once
void tryME()
{
}
class myclass
{
public:
myclass(void);
myclass(void);
void callTryME();
};
_________myclass.cpp____________
#include "myclass.h"
myclass::myclass(void)
{
}
myclass::~myclass(void)
{
}
void myclass::callTryME()
{
tryME();
}
This gives the error
1>myclass.obj : error LNK2005: "void __cdecl TryME(void)" (?TryME@@YAXXZ) already defined in tryout.obj 1>C:\tryout.exe : fatal error LNK1169: one or more multiply defined symbols found
If I declare the tryME() function as static, the problem is solved. But why?
I know that the .h file is included in the .cpp file and then compiled (into a translation unit) and that static variables and functions are visible to the entire translation unit they're contained into, but why doesn't the program work without the "static" keyword? The function tryME should be "global" outside the class and thus visible, isn't that correct? Does the calling put a "this->" before the tryME() ?