2014-10-06 101 views
1

以下MWE未能在VS 2008解析外部符號

Bar.h

struct Bar 
{ 
    Bar(); 
}; 

Bar.cpp

#include "Bar.h" 

Bar::Bar() 
{ 
} 

美孚編譯.h

#include "Bar.h" 

template<typename T> 
class Foo 
{ 
public: 
    Foo() 
    { 
    } 

private: 
    Bar m_bar; 
}; 

main.cp p

#include "Foo.h" 

int main() 
{ 
    Foo<int> f; 
    return 0; 
} 

錯誤消息:

error LNK2019: unresolved external symbol "public: __thiscall Bar::Bar(void)" ([email protected]@[email protected]) referenced in function "public: __thiscall Foo<int>::Foo<int>(void)" ([email protected]@@[email protected]) 

但是相同的代碼成功在編譯GCC 3.4.4 - 舊的編譯器,我知道,但它目前的最新版本,我已經有了我的機器。該代碼是否符合標準或者這是VS 2008的錯誤?

+2

愚蠢的問題,但你確定你添加了'Bar.cpp'到項目中嗎? – 2014-10-06 12:35:19

+0

不要忘記你的守衛。 – Niall 2014-10-06 12:42:30

回答

0

只要Bar.cpp也被編譯和鏈接,代碼是完全正確的,這是一個VC++ - 錯誤。檢查你是否正確設置了項目。