编辑:正如DyP在下面的注释中指出的,这只是c3(SoccerWorld)中函数定义的一个错误。
c1
,它有一个虚拟函数f
。c2
从c1
继承,另一个c3
从c2
继承。c2
没有虚拟函数f
,但是我想通过一些更改将它添加到c3
中。c1
中的虚拟函数c1
不是纯函数,而是在c1.cpp
中定义的。我仍然需要f
在c1
,以及。当将f
添加到c3
而不是c2
时,我会得到一个未解决的外部符号错误。如果我也将f
作为虚拟函数添加到c2
中,我会得到两个错误:一个和以前一样,另一个在c1.obj
中,其中说f
已经存在于c3
中。
c1
,c2
,c3
就是这样的:
class C1 {
virtual void f() { ... }
};
class C2 : public C1 {
//No virtual f
};
class C3 : public C2 {
virtual void f() { /*Do something*/ }
};
实f函数:
AbstractKart *World::createKart(const std::string &kart_ident, int index, int local_player_id, int global_player_id, RaceManager::KartType kart_type)
{ ... }
这是课堂上的世界。类WorldWithRank继承自World,并且没有createKart函数。类SoccerWorld从WorldWithRank继承而来,我希望它有createKart,这样如果它是SoccerWorld,那么我就可以不同地放置karts。
createKart在世界上受到保护。
错误:
world.obj : error LNK2005: "protected: virtual class AbstractKart * __thiscall World::createKart(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,int,int,int,enum RaceManager::KartType)" (?createKart@World@@MAEPAVAbstractKart@@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@HHHW4KartType@RaceManager@@@Z) already defined in soccer_world.obj
soccer_world.obj : error LNK2001: unresolved external symbol "protected: virtual class AbstractKart * __thiscall SoccerWorld::createKart(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,int,int,int,enum RaceManager::KartType)" (?createKart@SoccerWorld@@MAEPAVAbstractKart@@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@HHHW4KartType@RaceManager@@@Z)
发布于 2013-07-21 11:10:00
从对“任择议定书”的评论中转载:
从你的错误来看,我认为你更喜欢打错字。在soccer_world.cpp
中,您定义了一个函数AbstractKart *World::createKart(...)
,而不是AbstractKart *SoccerWorld::createKart(...)
。
这将创建AbstractKart *World::createKart(...)
的第二个定义,该定义解释了第一个错误:
world.obj : error LNK2005:"
protected: virtual class AbstractKart * __thiscall World::createKart([...])
“.已经在soccer_world.obj中定义
如果尝试调用未定义的AbstractKart *SoccerWorld::createKart(...)
,则会发生第二个错误。
soccer_world.obj :错误LNK2001:未解决的外部符号"
protected: virtual class AbstractKart * __thiscall SoccerWorld::createKart([...])
“
https://stackoverflow.com/questions/17775402
复制