编译C++代码时出错?

aks*_*aks 0 c++ gcc compiler-errors g++

这是我的test.cpp:

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

int main()
{
C obj;
return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我使用该命令编译它时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$ 

Gre*_*ill 10

你已宣布存在的的C构造函数和析构函数,但没有提供实现.尝试:

class C {
public:
    C() {}
    ~C() {}
};
Run Code Online (Sandbox Code Playgroud)

而且,对于C++程序,使用g++编译(如第一次尝试).


Aru*_*run 9

更换

#include <iostream.h>
Run Code Online (Sandbox Code Playgroud)

通过

#include <iostream>
Run Code Online (Sandbox Code Playgroud)

并提供类C的构造函数和析构函数的实现,至少是空的.


Tim*_*sch 5

由于您没有提供实际问题,我不得不猜测您想知道什么.无论如何,我的2c是:

  • 不要使用iostream.h,标题是预先标准的并且已经过时了.请<iostream>改用
  • 您没有为构造函数和析构函数提供任何实现C,这是链接器所抱怨的.