Function declared but not defined? But 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)' is declared but not defined 1 > c: \ programming **** \ vectormath.h (19): see 'VectorMath :: Make' declaration

The error points me to the xstring line (part of the standard line) of line 541, which seems to have nothing to do with anything at all.

I would like to note that removing the โ€œstaticโ€ gives me linker errors telling me that โ€œMakeโ€ is an unresolved external character ...

+6
source share
3 answers

You need to remove static , because otherwise the function will not be visible in different compilation units. Just use

 namespace VectorMath { FVector Make(float X, float Y, float Z); } 

as well as for definition.

If this does not solve your binding problem, you need to make sure that you really compile and link file.cpp correctly, but static definitely wrong.


Regarding your comment, that you found a problem that you cannot separate the declaration from the definition when using inline functions: Yes, it looks like the generated symbol of the method and its visibility, What I find strange is that that you request this as a precondition to accept the answer, although you never mentioned inline in your question. How can I even know that you are simply adding random keywords that you really don't understand? This is not a good base for others to help you deal with your problems. You must publish the real code and be honest with us. Please keep this in mind if you ask more questions in the future.

+11
source

If this helps, the code works in one compilation unit

http://codepad.org/mHyB5nEl

 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 
+1
source

You must abandon the โ€œstaticโ€ in the definition. In any case, there is no reason for this feature to be static. This way you can also remove it in the ad.

So you can write a definition just 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; } } 

Greetings

-2
source

Source: https://habr.com/ru/post/956787/


All Articles