Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function Declared But Not Defined? Yet It Is Defined

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...

like image 833
Slight Avatar asked Oct 26 '13 16:10

Slight


People also ask

Can objects of class type that is declared but not defined be created?

An incomplete class declaration is a class declaration that does not define any class members. You cannot declare any objects of the class type or refer to the members of a class until the declaration is complete.

Is declared but never defined?

A "declared but not defined" error usually means you've declared a static function in a file but never actually defined the function. It usually isn't a linker error. The linker error would normally be 'undefined reference'.


1 Answers

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.

like image 182
Daniel Frey Avatar answered Nov 11 '22 15:11

Daniel Frey