Reputation: 1582
header.h
namespace VectorMath {
static FVector Make(float X, float Y, float Z);
}
file.cpp
namespace VectorMath {
static FVector Make(float X, float Y, float Z)
{
FVector ret;
ret.X = X;
ret.Y = Y;
ret.Z = Z;
return ret;
}
}
error
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\xstring(541): error C2129: static function 'FVector VectorMath::Make(float,float,float)' declared but not defined 1> c:\programming****\vectormath.h(19) : see declaration of 'VectorMath::Make'
The error is pointing me to xstring (part of the standard string library) line 541 which seems to bare no relevance to anything at all.
I'd like to note that removing "static" gives me linker errors telling me "Make" is an unresolved external symbol...
Upvotes: 8
Views: 11249
Reputation: 56883
You need to remove the static
, as otherwise the function will not be visible across different compilation units. Just use
namespace VectorMath {
FVector Make(float X, float Y, float Z);
}
and likewise for the definition.
If this doesn't solve your linking problem, you need to make sure you actually compile and link file.cpp
properly, but the static
is definitely wrong.
Regarding your comment that you found the problem, which was that you can't separate the declaration from the definition when using inline
-functions: Yes, that has a similar effect to the generated symbol of the method and its visibility. What I find strange is that you request this as a precondition to accept the answer although you never mentioned inline
in your question. How would I even know that you just add random keywords which you don't really understand? This is not a good base for others to help you with your problems. You need to post the real code and be honest with us. Please keep this in mind if asking more questions in the future.
Upvotes: 13
Reputation: 3075
If it helps , the code works in a single compilation unit
namespace VectorMath {
class FVector{
public:
float X;
float Y;
float Z;
void show (){
std::cout<< "\n \t" <<X << "\t "<< Y <<"\t "<<Z;
}
};
static FVector Make(float X, float Y, float Z);
}
namespace VectorMath {
static FVector Make(float X, float Y, float Z)
{
FVector ret;
ret.X = (float)X;
ret.Y = (float)Y;
ret.Z = (float)Z;
return ret;
}
}
int main()
{
VectorMath::FVector result = VectorMath :: Make(float(1.2) , float(2.2) ,float(4.2));
result.show();
}
output :
1.2 2.2 4.2
Upvotes: 1
Reputation: 494
You must drop the "static" in the definition, Anyway there's no reason for this function to be static. So you can also drop it in the declaration.
So you can write the definition it both like this:
FVector VectorMath::Make(float X, float Y, float Z)
{
FVector ret;
ret.X = X;
ret.Y = Y;
ret.Z = Z;
return ret;
}
and this:
namespace VectorMath
{
FVector Make(float X, float Y, float Z)
{
FVector ret;
ret.X = X;
ret.Y = Y;
ret.Z = Z;
return ret;
}
}
Cheers
Upvotes: -2