CS50 - C ++:" using namespace std;"

时间:2018-06-07 17:19:09

标签: c++ namespaces std using

我是C ++的新手,我将这完全从我的教科书复制到CS50。

#include <iostream>

using namespace std;

int main () {

    cout << "My first C++ program." << endl;
    cout << "The sum of 2 and 3 = " << 5 << endl;
    cout << "7+8 =" << 7+8 <<endl;

    return 0;
}

当我使用它们编译时:gcc sample.cpp -g -o sample

我收到以下错误:

/tmp/ccaTrTqq.o: In function `main':
/home/ubuntu/workspace/Chapter 1/sample.cpp:7: undefined reference to `std::cout'
/home/ubuntu/workspace/Chapter 1/sample.cpp:7: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/home/ubuntu/workspace/Chapter 1/sample.cpp:7: 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> >&)'
/home/ubuntu/workspace/Chapter 1/sample.cpp:7: undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
/home/ubuntu/workspace/Chapter 1/sample.cpp:8: undefined reference to `std::cout'
/home/ubuntu/workspace/Chapter 1/sample.cpp:8: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/home/ubuntu/workspace/Chapter 1/sample.cpp:8: undefined reference to `std::ostream::operator<<(int)'
/home/ubuntu/workspace/Chapter 1/sample.cpp:8: 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> >&)'
/home/ubuntu/workspace/Chapter 1/sample.cpp:8: undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
/home/ubuntu/workspace/Chapter 1/sample.cpp:9: undefined reference to `std::cout'
/home/ubuntu/workspace/Chapter 1/sample.cpp:9: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/home/ubuntu/workspace/Chapter 1/sample.cpp:9: undefined reference to `std::ostream::operator<<(int)'
/home/ubuntu/workspace/Chapter 1/sample.cpp:9: 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> >&)'
/home/ubuntu/workspace/Chapter 1/sample.cpp:9: undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
/tmp/ccaTrTqq.o: In function `__static_initialization_and_destruction_0':
/usr/include/c++/4.8/iostream:74: undefined reference to `std::ios_base::Init::Init()'
/usr/include/c++/4.8/iostream:74: undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status

0 个答案:

没有答案