Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 字符串的简单使用,从C++;_C++_String_C++11 - Fatal编程技术网

C++ 字符串的简单使用,从C++;

C++ 字符串的简单使用,从C++;,c++,string,c++11,C++,String,C++11,嗯 自从我写过C++以来,已经有一段时间了。 我生锈了 那么我做错了什么,为什么 #include <iostream> std::string hello() { return "another green world"; } int main(int argc, char **argv) { std::cout << hello() << std::endl; return 0; } 给我 /tmp/ccxCCo47.o: In

自从我写过C++以来,已经有一段时间了。 我生锈了

那么我做错了什么,为什么

#include <iostream>

std::string hello() {
  return "another green world";
}

int main(int argc, char **argv) {
  std::cout << hello() << std::endl;    
  return 0;
}
给我

/tmp/ccxCCo47.o: In function `hello[abi:cxx11]()':
test.cpp:(.text+0x34): undefined reference to `std::allocator<char>::allocator()'
test.cpp:(.text+0x4b): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)'
test.cpp:(.text+0x57): undefined reference to `std::allocator<char>::~allocator()'
test.cpp:(.text+0x7b): undefined reference to `std::allocator<char>::~allocator()'
/tmp/ccxCCo47.o: In function `main':
test.cpp:(.text+0xcb): undefined reference to `std::cout'
test.cpp:(.text+0xd0): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char>, std::allocator<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
test.cpp:(.text+0xda): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
test.cpp:(.text+0xe5): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
test.cpp:(.text+0xf1): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
test.cpp:(.text+0x116): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
/tmp/ccxCCo47.o: In function `__static_initialization_and_destruction_0(int, int)':
test.cpp:(.text+0x156): undefined reference to `std::ios_base::Init::Init()'
test.cpp:(.text+0x16b): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccxCCo47.o:(.data.rel.local.DW.ref.__gxx_personality_v0[DW.ref.__gxx_personality_v0]+0x0): undefined reference to `__gxx_personality_v0'
collect2: error: ld returned 1 exit status
/tmp/ccxCCo47.o:在函数“hello[abi:cxx11]()”中:
test.cpp:(.text+0x34):对“std::allocator::allocator()”的未定义引用
test.cpp:(.text+0x4b):对“std::\uuucx11::basic\u string::basic\u string(char const*,std::allocator const&)”的未定义引用
test.cpp:(.text+0x57):对“std::allocator::~allocator()”的未定义引用
test.cpp:(.text+0x7b):对“std::allocator::~allocator()”的未定义引用
/tmp/ccxCCo47.o:在函数“main”中:
test.cpp:(.text+0xcb):对“std::cout”的未定义引用

test.cpp:(.text+0xd0):对“std::basic\u ostream&std::operator”的未定义引用您需要包含头文件

#include <iostream>

如何编译你的代码?用ITS<代码> G+++ <代码>更新问题,而不是<代码> GCC >编译C++代码>code>g++test.cpp-o test
。您使用哪种编译器?因为它似乎是用ideone工作的。我真的在这样做。我只是忘了把它包括在上面。试着用g++编译吗?g++test.cpp有什么区别?如果使用g++,默认情况下将链接libstdc++。通过@星际GCC是C编译器,用G++编译C++。他们不是同一种语言。
#include <iostream>
g++ test.cpp