功能已声明但未定义?然而它被定义了

Sli*_*ght 6 c++ visual-studio

header.h

namespace VectorMath {
    static FVector Make(float X, float Y, float Z);
}
Run Code Online (Sandbox Code Playgroud)

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

错误

1> c:\ program files(x86)\ microsoft visual studio 10.0\vc\include\xstring(541):错误C2129:静态函数'FVector VectorMath :: Make(float,float,float)'声明但未定义1> c:\ programming****\vectormath.h(19):参见'VectorMath :: Make'的声明

错误指向xstring(标准字符串库的一部分)第541行,它似乎与任何东西都没有任何关系.

我想要注意,删除"静态"会给我链接器错误,告诉我"Make"是一个未解析的外部符号......

Dan*_*rey 11

您需要删除static,否则该函数将在不同的编译单元中不可见.只是用

namespace VectorMath {
    FVector Make(float X, float Y, float Z);
}
Run Code Online (Sandbox Code Playgroud)

同样的定义.

如果这不能解决您的链接问题,您需要确保实际编译和链接file.cpp正确,但这static绝对是错误的.


关于您发现问题的注释,即使用inline-functions 时无法将声明与定义分开:是,它对生成的方法符号及其可见性具有类似的效果.我觉得奇怪的是你要求这个作为接受答案的先决条件,尽管你inline在问题中从未提及过.我怎么会知道你只是添加了你不太懂的随机关键词?这不是帮助您解决问题的其他人的良好基础.您需要发布真实的代码并对我们诚实.如果将来提出更多问题,请记住这一点.