【问题标题】:The #include<iostream> exists, but I get an error: identifier "cout" is undefined. Why?#include<iostream> 存在,但出现错误:标识符“cout”未定义。为什么?
【发布时间】:2012-10-23 21:33:18
【问题描述】:

我通过书籍学习 C++ 和 COM。 在 IDE MS Visual Studio 2012 中,我创建了新的空 C++ 项目,并向其中添加了一些现有文件。我的 CPP 文件包含 #include&lt;iostream&gt; 行,但在编辑器中我收到了这样的消息:

错误:标识符“cout”未定义

结束

错误:标识符“endl”未定义

代码:

#include<iostream>
#include"interfaces.h" // unknown.h, objbase.h, initguid.h

class CA {//: public IX, IY{
public:
    // Constructor
    CA();
    // Destructor
    ~CA();
    // IUnknown
    virtual HRESULT __stdcall QueryInterface(const IID& iid, void** ppv);
    virtual ULONG __stdcall AddRef();
    virtual ULONG __stdcall Release();
    // IX
    virtual void __stdcall Fx1();
    virtual void __stdcall Fx2();
    // IY
    virtual void __stdcall Fy1(){ cout << "Fy1" << endl; }  // errors here
    virtual void __stdcall Fy2(){ cout << "Fy2" << endl; }  // errors here also
private:
    long counter;
};

为什么会这样?

【问题讨论】:

  • 不知道“使用命名空间标准;”的新鲜和纯洁的头脑。甜:)
  • 鉴于这个问题在谷歌上的排名相当高,我认为用实际的 MCVE 替换这张代码图片是值得的。

标签: c++ visual-studio com visual-studio-2012


【解决方案1】:

你需要指定std::命名空间:

std::cout << .... << std::endl;;

或者,您可以使用using 指令:

using std::cout;
using std::endl;

cout << .... << endl;

我应该补充一点,您应该避免在标头中使用这些 using 指令,因为包含这些的代码也会将符号带入全局命名空间。将 using 指令限制在小范围内,例如

#include <iostream>

inline void foo()
{
  using std::cout;
  using std::endl;
  cout << "Hello world" << endl;
}

这里,using 指令只适用于foo() 的范围。

【讨论】:

  • 谢谢!我忘记了。 :) 现在一切正常。
【解决方案2】:

您可以在#include &lt;iostream&gt;之后的开头添加这个:

using namespace std;

【讨论】:

    【解决方案3】:

    cout 在 std 命名空间中,您应在代码中使用 std::cout。 并且不要在头文件中添加using namespace std;,将代码与std命名空间混合是不好的,尤其是不要在头文件中添加。

    【讨论】:

    • 你的意思是“不在你的头文件中添加 using namespace std;”,你的意思是在头文件之后,对吧?但为什么?每个人都使用“#include<...> using namespace std;”。这样做有什么好做的吗?
    【解决方案4】:

    问题是您缺少 std 命名空间。 cout 在 std 命名空间中。
    在#include 后添加using namespace std;

    【讨论】:

      【解决方案5】:

      如果您包含 #include iostreamusing namespace std;,它应该可以工作。如果它仍然不起作用,请确保检查您没有删除 iostream 文件中的任何内容。要获取 iostream 文件,只需 Ctrl +单击您的 #include iostream,它就会将您带到该文件。您可以将以下原始 iostream 文件粘贴到您的 iostream 文件中,它应该可以工作。

      // Standard iostream objects -*- C++ -*-
      
      // Copyright (C) 1997-2019 Free Software Foundation, Inc.
      //
      // This file is part of the GNU ISO C++ Library.  This library is free
      // software; you can redistribute it and/or modify it under the
      // terms of the GNU General Public License as published by the
      // Free Software Foundation; either version 3, or (at your option)
      // any later version.
      
      // This library is distributed in the hope that it will be useful,
      // but WITHOUT ANY WARRANTY; without even the implied warranty of
      // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      // GNU General Public License for more details.
      
      // Under Section 7 of GPL version 3, you are granted additional
      // permissions described in the GCC Runtime Library Exception, version
      // 3.1, as published by the Free Software Foundation.
      
      // You should have received a copy of the GNU General Public License and
      // a copy of the GCC Runtime Library Exception along with this program;
      // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
      // <http://www.gnu.org/licenses/>.
      
      /** @file include/iostream
       *  This is a Standard C++ Library header.
       */
      
      //
      // ISO C++ 14882: 27.3  Standard iostream objects
      //
      
      #ifndef _GLIBCXX_IOSTREAM
      #define _GLIBCXX_IOSTREAM 1
      
      #pragma GCC system_header
      
      #include <bits/c++config.h>
      #include <ostream>
      #include <istream>
      
      namespace std _GLIBCXX_VISIBILITY(default)
      {
      _GLIBCXX_BEGIN_NAMESPACE_VERSION
      
        /**
         *  @name Standard Stream Objects
         *
         *  The &lt;iostream&gt; header declares the eight <em>standard stream
         *  objects</em>.  For other declarations, see
         *  http://gcc.gnu.org/onlinedocs/libstdc++/manual/io.html
         *  and the @link iosfwd I/O forward declarations @endlink
         *
         *  They are required by default to cooperate with the global C
         *  library's @c FILE streams, and to be available during program
         *  startup and termination. For more information, see the section of the
         *  manual linked to above.
        */
        //@{
        extern istream cin;       /// Linked to standard input
        extern ostream cout;      /// Linked to standard output
        extern ostream cerr;      /// Linked to standard error (unbuffered)
        extern ostream clog;      /// Linked to standard error (buffered)
      
      #ifdef _GLIBCXX_USE_WCHAR_T
        extern wistream wcin;     /// Linked to standard input
        extern wostream wcout;    /// Linked to standard output
        extern wostream wcerr;    /// Linked to standard error (unbuffered)
        extern wostream wclog;    /// Linked to standard error (buffered)
      #endif
        //@}
      
        // For construction of filebuffers for cout, cin, cerr, clog et. al.
        static ios_base::Init __ioinit;
      
      _GLIBCXX_END_NAMESPACE_VERSION
      } // namespace
      
      #endif /* _GLIBCXX_IOSTREAM */
      

      【讨论】:

        猜你喜欢
        • 2015-05-21
        • 1970-01-01
        • 2013-04-30
        • 2013-04-01
        • 1970-01-01
        • 2018-04-10
        • 2013-10-05
        • 2010-12-24
        • 1970-01-01
        相关资源
        最近更新 更多