8

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

4
  • 1
    Your definition isn't the same. You have a function declared in the namespace and a different one defined out of the namespace.
    – chris
    Commented Oct 26, 2013 at 16:48
  • Did you implement Make in the namespace VectorMath
    – drescherjm
    Commented Oct 26, 2013 at 16:48
  • I actually did put them in the same namespace, sorry correcting question.
    – Slight
    Commented Oct 26, 2013 at 16:50
  • Remove the static from both the declaration and the definition.
    – FKaria
    Commented Oct 26, 2013 at 17:07

3 Answers 3

13

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.

7
  • Right, as I noted though removing static gives me unresolved symbols whenever I reference VectorMath::Make in another .cpp file. Any ideas as to why? Do I have to use a static class or something other than just functions in a namespace?
    – Slight
    Commented Oct 26, 2013 at 17:14
  • @Slight As already written in the answer: Without the static the code itself is fine. You need to fix how the project is build. Since I'm not using Visual Studio, I can only point in the right direction: AFAIK you need to add the file file.cpp as a source to the project - but maybe you need to ask a VS user... Commented Oct 26, 2013 at 17:16
  • I assure you if you create the two files in my question, add them to a solution in MSVS, removing the static keyword. Then create a third .cpp file that includes the vectormath.h header, then try and use one of the functions in that namespace, you will get linker errors. Also you keep saying "how" I build the program. It's a very basic dll build, nothing fancy or wrong going on here. I believe using the extern keyword has actually solved my issue, though I want to test to be sure.
    – Slight
    Commented Oct 26, 2013 at 17:23
  • I'm just going to mark this as correct and post a separate question then.
    – Slight
    Commented Oct 26, 2013 at 17:33
  • I found the solution to the linker problem. You cannot separate the declaration from the definition when using inline functions. If you include this in your answer, I will mark it as correct.
    – Slight
    Commented Oct 26, 2013 at 17:44
1

If it helps , the code works in a single 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
  • That doesn't help. The code is fine with or without static in a single compilation unit, and wrong with static and otherwise fine when used in projects with multiple compilation unit. It seems the OPs real problem is how he builds the program(s). Commented Oct 26, 2013 at 17:20
-2

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

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.