自从我多年前意识到,这不会默认产生一个错误,(至less在gcc中)我总是想知道为什么? 我明白,你可以发出编译器标志来产生警告,但不应该总是一个错误? 为什么没有返回值是有效的非void函数是有意义的? 评论中所要求的一个例子: #include <stdio.h> int stringSize() { } int main() { char cstring[5]; printf( "the last char is: %c\n", cstring[stringSize()-1] ); return 0; } …编译。
任何人都可以解释为什么下面的代码不会编译 至less在g ++ 4.2.4上。 而更有趣的是,为什么在我将MEMBER转换为int时会编译? #include <vector> class Foo { public: static const int MEMBER = 1; }; int main(){ vector<int> v; v.push_back( Foo::MEMBER ); // undefined reference to `Foo::MEMBER' v.push_back( (int) Foo::MEMBER ); // OK return 0; }
所以,我得到了下面的代码(这个类是CGameModule)的臭名昭着的可怕的“未定义的参考”vtable …“的错误,我不能为了我的生活理解问题是什么。 起初,我以为是忘记把一个虚拟的功能当作一个机构,但据我所知,一切都在这里。 继承链有点长,但是这里是相关的源代码。 我不确定我应该提供哪些其他信息。 注意:构造函数就是这个错误发生的地方,看起来似乎。 我的代码: class CGameModule : public CDasherModule { public: CGameModule(Dasher::CEventHandler *pEventHandler, CSettingsStore *pSettingsStore, CDasherInterfaceBase *pInterface, ModuleID_t iID, const char *szName) : CDasherModule(pEventHandler, pSettingsStore, iID, 0, szName) { g_pLogger->Log("Inside game module constructor"); m_pInterface = pInterface; } virtual ~CGameModule() {}; std::string GetTypedTarget(); std::string GetUntypedTarget(); bool DecorateView(CDasherView *pView) { //g_pLogger->Log("Decorating the view"); return […]