2010-03-02 240 views
0

這是我test.cpp編譯C++代碼時出錯?

#include <iostream.h> 
class C { 
public: 
C(); 
~C(); 
}; 

int main() 
{ 
C obj; 
return 0; 
} 

當我編譯使用命令g++ test.cpp它,我得到這個錯誤信息:

 
    In file included from /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/backward/iostream.h:31, 
        from test.cpp:1: 
    /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the header for the header for C++ includes, or instead of the deprecated header . To disable this warning use -Wno-deprecated. 
    /cygdrive/c/Users/aswinik_sattaluri/AppData/Local/Temp/ccoYkiAS.o:test.cpp:(.text+0x131): undefined reference to `C::C()' 
    /cygdrive/c/Users/aswinik_sattaluri/AppData/Local/Temp/ccoYkiAS.o:test.cpp:(.text+0x13c): undefined reference to `C::~C()' 
    collect2: ld returned 1 exit status 

gcc test.cpp編譯給出類似的消息,甚至更多:

 
    In file included from /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/backward/iostream.h:31, 
        from test.cpp:1: 
    /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the header for the header for C++ includes, or instead of the deprecated header . To disable this warning use -Wno-deprecated. 
    /cygdrive/c/Users/aswinik_sattaluri/AppData/Local/Temp/cc3ntGx0.o:test.cpp:(.text+0xd): undefined reference to `std::basic_string, std::allocator >::size() const' 
    /cygdrive/c/Users/aswinik_sattaluri/AppData/Local/Temp/cc3ntGx0.o:test.cpp:(.text+0x60): undefined reference to `std::basic_string, std::allocator >::operator[](unsigned int) const' 
    /cygdrive/c/Users/aswinik_sattaluri/AppData/Local/Temp/cc3ntGx0.o:test.cpp:(.text+0x9f): undefined reference to `std::basic_string, std::allocator >::operator[](unsigned int) const' 
    /cygdrive/c/Users/aswinik_sattaluri/AppData/Local/Temp/cc3ntGx0.o:test.cpp:(.text+0xce): undefined reference to `std::basic_string, std::allocator >::operator[](unsigned int) const' 
    /cygdrive/c/Users/aswinik_sattaluri/AppData/Local/Temp/cc3ntGx0.o:test.cpp:(.text+0x131): undefined reference to `C::C()' 
    /cygdrive/c/Users/aswinik_sattaluri/AppData/Local/Temp/cc3ntGx0.o:test.cpp:(.text+0x13c): undefined reference to `C::~C()' 
    /cygdrive/c/Users/aswinik_sattaluri/AppData/Local/Temp/cc3ntGx0.o:test.cpp:(.text+0x165): undefined reference to `std::ios_base::Init::Init()' 
    /cygdrive/c/Users/aswinik_sattaluri/AppData/Local/Temp/cc3ntGx0.o:test.cpp:(.text+0x180): undefined reference to `std::ios_base::Init::~Init()' 
    collect2: ld returned 1 exit status 

請注意,我沒有設置LD_LIBRARY_PATH

 
    bash-3.2$ echo $LD_LIBRARY_PATH 

    bash-3.2$ 
+1

正確格式化您的代碼(請使用下次預覽!)。仍然 - 問題是什麼? – 2010-03-02 07:39:15

+0

所有標準的C++頭文件都沒有擴展名。 – GManNickG 2010-03-02 08:04:41

回答

10

聲明該存在的構造函數和析構函數,但沒有提供實現。嘗試:

class C { 
public: 
    C() {} 
    ~C() {} 
}; 

而且,對於C++程序,用g++編譯(在你的第一次嘗試)。

4

您需要定義你的C構造函數和destuctor:

C::C() 
{ 
} 

C::~C() 
{ 
} 

此外,堅持用G ++編譯。如果仔細觀察,使用gcc進行編譯時出現的錯誤包括使用g ++獲得的所有內容以及額外的錯誤。

9

更換

#include <iostream.h> 

通過

#include <iostream> 

,並提供實現,至少空,C類的構造函數和析構函數的

4

您包含iostream.h而不是iostream,這就是爲什麼您會收到關於此的警告。你也聲明瞭一個構造函數和一個析構函數,但你並沒有在任何地方實現它。因此,鏈接器抱怨未定義的符號。

您需要添加實現爲C的方法,如:

C::C() { 
    // ... 
} 
5

因爲你不提供一個實際的問題,我有你想知道什麼猜測。無論如何,我的2c是:

  • 不要使用iostream.h,該標頭是預標準的,並且過時。使用<iostream>而不是
  • 您不提供任何構造函數和析構函數的實現,它是鏈接器所抱怨的C。而不是#include <iostream.h>

+1

+1,第一個人說*爲什麼*你應該更喜歡'iostream'到'iostream.h',而不是僅僅指出你應該。 – 2010-03-02 08:53:47

0

使用#include <iostream>你應該正確讀取錯誤。