<分区>
我有以下文件:
接口(interface).h
class Interface
{
...
};
Interface* CreateInterface();
实现.h
#include "interface.h"
class Impl: public Interface
{
...
};
实现.cpp
#include "impl.h"
SomeInterface* CreateInterface()
{
return new Impl;
}
...
主要.cpp
#include "Interface.h"
int main()
{
CreateInterface();
}
最初我的项目看起来很喜欢 this .但出于测试目的,所有文件现在都在同一个项目中。
编译时出现 LNK2019 错误,提示 CreateInterface 未解析。我正在使用 VS 2008,我不知道出了什么问题。
您是否打算拥有多个 CreateInterface()
声明?
接口(interface).h
Interface* CreateInterface();
Impl.cpp
SomeInterface* CreateInterface()
其中只有一个(后者)似乎被实际实现了。您的名称含糊不清,编译器似乎选择了错误的名称(很可能是因为它甚至不知道另一个名称是否存在)。
在 impl.cpp 中使用 相同 参数列表和返回类型定义 CreateInterface()
作为 Interface.h 中的原型(prototype)。我通常不会容忍这种野蛮的设计,但看起来这就是你想要的。
Impl.cpp
Interface* CreateInterface() // note the return type
{
return new Impl;
}
老实说,我不敢相信这甚至可以编译,考虑到你只是两者之间的区别(Interface.h 中的声明和 Impl.cpp 中的实现)是返回类型的区别,编译器应该吐个遍那,所以您可能还没有向我们展示一些东西。