45

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

错误:标识符“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;
};

为什么会发生?

4

5 回答 5

58

您需要指定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().

于 2012-11-03T11:04:17.920 回答
14

您可以在开头添加以下内容#include <iostream>

using namespace std;
于 2014-02-25T16:03:30.453 回答
9

cout位于 std 命名空间中,您应std::cout在代码中使用。而且你不应该using namespace std;在你的头文件中添加,将你的代码与 std 命名空间混合是不好的,特别是不要将它添加到头文件中。

于 2012-11-03T11:04:31.677 回答
2

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

于 2020-08-21T16:17:07.353 回答
0

如果您已包含它#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 */
于 2021-10-31T13:30:41.420 回答