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;
}
}误差
1>c:\program文件(x86)\microsoft visual studio 10.0\vc\include\xstring(541):error C2129: FVector VectorMath::Make(浮点、浮点、浮点数)声明的但未定义的1> c:\programming*\vetormath.h(19):参见“VectorMath::Make”的声明
这个错误将我指向xstring (标准字符串库的一部分)第541行,它似乎与任何事情都没有任何关联。
我想指出,删除“静态”会给我链接器错误,告诉我"Make“是一个未解决的外部符号。
发布于 2013-10-26 17:05:35
您需要删除static,否则函数在不同的编译单元之间是不可见的。只管用
namespace VectorMath {
FVector Make(float X, float Y, float Z);
}同样的定义也是如此。
如果这不能解决您的链接问题,则需要确保您确实正确地编译和链接了file.cpp,但是static显然是错误的。
关于您发现问题的评论,即在使用inline-functions时,您无法将声明与定义分开:是的,这与方法的生成符号及其可见性具有类似的效果。我发现奇怪的是,尽管您在问题中从未提到过inline,但您要求将此作为接受答案的先决条件。我怎么知道你只是随机添加了一些你不太明白的关键词呢?这不是一个很好的基础,别人帮助你解决你的问题。你需要贴出真正的代码并对我们诚实。如果将来有更多的问题,请记住这一点。
发布于 2013-10-26 17:17:46
如果有帮助,代码只在一个编译单元中工作。
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();
}产出:
1.2 2.2 4.2发布于 2013-10-26 16:53:03
您必须删除定义中的“静态”,无论如何,这个函数没有理由是静态的。所以你也可以把它放在声明中。
所以你可以把它写成这样:
FVector VectorMath::Make(float X, float Y, float Z)
{
FVector ret;
ret.X = X;
ret.Y = Y;
ret.Z = Z;
return ret;
}这是:
namespace VectorMath
{
FVector Make(float X, float Y, float Z)
{
FVector ret;
ret.X = X;
ret.Y = Y;
ret.Z = Z;
return ret;
}
}干杯
https://stackoverflow.com/questions/19609239
复制相似问题