【问题标题】:Trouble running C++ through terminal in Ubuntu在 Ubuntu 中通过终端运行 C++ 时遇到问题
【发布时间】:2015-08-21 23:23:47
【问题描述】:

我在使用“gedit take_input.cpp”后写了如下代码:

#include <iostream>

int main() 
{
cout<<"please enter your name (followed by 'enter')\n";

string file;

cin >> file;

cout<<"hello" << file << " ! welcome to ilinux, where innovation is a promise\n";
}

但是,当我使用“g++”将我的人类可读代码转换为目标代码(编写g++ take_input.cpp -o take_input)时,终端返回的结果类似于:

take_input.cpp: In function ‘int main()’:
take_input.cpp:5:1: error: ‘cout’ was not declared in this scope
 cout<<"please enter your name (followed by 'enter')\n";
 ^
take_input.cpp:5:1: note: suggested alternative:
In file included from take_input.cpp:1:0:
/usr/include/c++/4.9/iostream:61:18: note:   ‘std::cout’
   extern ostream cout;  /// Linked to standard output
                  ^
take_input.cpp:7:1: error: ‘string’ was not declared in this scope
 string file;
 ^
take_input.cpp:7:1: note: suggested alternative:
In file included from /usr/include/c++/4.9/iosfwd:39:0,
                 from /usr/include/c++/4.9/ios:38,
                 from /usr/include/c++/4.9/ostream:38,
                 from /usr/include/c++/4.9/iostream:39,
                 from take_input.cpp:1:
/usr/include/c++/4.9/bits/stringfwd.h:62:33: note:   ‘std::string’
   typedef basic_string<char>    string;   
                                 ^
take_input.cpp:9:1: error: ‘cin’ was not declared in this scope
 cin >> file;
 ^            ^
take_input.cpp:9:8: error: ‘file’ was not declared in this scope
 cin >> file;
take_input.cpp:9:1: note: suggested alternative:
In file included from take_input.cpp:1:0:
/usr/include/c++/4.9/iostream:60:18: note:   ‘std::cin’
   extern istream cin;  /// Linked to standard input
                  ^
take_input.cpp:9:8: error: ‘file’ was not declared in this scope
 cin >> file;
        ^

你能告诉我是什么原因吗?

【问题讨论】:

  • 你必须写std::cout,因为它在std命名空间中。
  • 顺便说一句,你可以很高兴你没有被允许发布图片。我敢肯定,如果你这样做,你会得到更多的反对票。图片不可复制/粘贴,因此当您有一些代码时,请始终将其作为文本而不是图片。
  • @SadmanSakib “我不明白。为什么我的问题被否决了?” 因为您的研究工作量很低。非常基本的教程和 C++ 教科书已经涵盖了这一点。
  • @SadmanSakib De nada。您也可以再次浏览The tour,并阅读Help Center 中的文章。

标签: c++ linux ubuntu gcc terminal


【解决方案1】:

只需阅读编译器给您的错误消息。问题是

'cout' 没有在这个范围内声明

“建议的替代方案”是std::coutstringstd::string 也是如此。

注意,一般情况下,属于标准库的东西需要用std::限定才能找到。

顺便说一句,您还需要#include &lt;string&gt; 才能使用std::string

【讨论】:

    【解决方案2】:

    只需添加

    using namespace std;
    

    #include &lt;iostream&gt;之后

    试试这个。

    【讨论】:

    • 我没有投反对票,因为这并没有错,而是please do not teach that to beginners
    • 您能解释一下原因吗?我也是初学者,没听懂你的回答。你能澄清一下吗?
    • @RafizSalehinSejim 恕我直言,这不是一个错误,对于小程序来说,using namespace std; 完全可以。然而,人们应该意识到潜在的问题。实际上,我在很多书籍和演讲中都看到了这种做法。小例子 sn-ps 可能没问题,但他们经常忘记提及有充分的理由不这样做。
    • @RafizSalehinSejim using namespace std; 不是终极邪恶。如果你例如需要呈现短代码sn-ps(就像你演讲时一样),我个人还是不喜欢它,但不会造成伤害。然而,人们应该了解其中的危险,这是初学者通常无法做到的。因此我说:不要告诉他们这样做。
    • @BaummitAugen 非常感谢!我保证我不会
    【解决方案3】:

    编译器在第 7 行给出答案:由于您没有使用 std 命名空间,因此您必须在 coutcin 调用之前添加 std::

    【讨论】:

      【解决方案4】:

      您遇到的错误是因为 cout 不在全局命名空间中,而是在 std 命名空间中。

      不用写了

      using namespace std;
      

      #include &lt;iostream&gt; 之后尝试使用:

      using std::cout;
      

      因为使用第一个选项是一种不好的做法。可以参考Why is using namespace std is a bad practice. 有关使用using std::cout 的好处,请参阅Using std namespace

      如果您不想使用using std::cout,也可以在任何地方使用std::cout

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-13
        • 2010-09-20
        • 1970-01-01
        • 2022-10-12
        • 2021-05-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多