代码之家  ›  专栏  ›  技术社区  ›  Wallter

错误C2065:“cout”:未声明的标识符

  •  64
  • Wallter  · 技术社区  · 16 年前

    我正在做编程作业中的“驱动程序”部分,我一直收到这个荒谬的错误:

    我甚至试过用 但我得到另一个错误: IntelliSense:命名空间“std”没有成员“cout” 当我宣布 ostream公司

    #include <iostream>
    using namespace std;
    
    int main () {
        cout << "hey" << endl;
     return 0;
    }
    

    我正在使用VisualStudio2010并运行Windows7。所有的.h文件都有“using namespace std”,包括iostream和ostream。

    24 回复  |  直到 13 年前
        1
  •  65
  •   George Chondrompilas    7 年前

    在Visual Studio中,必须 #include "stdafx.h" cpp文件的第一个include。

    不 工作。

    #include <iostream>
    using namespace std;
    int main () {
        cout << "hey" << endl;
        return 0;
    }
    
    
    
    
    #include <iostream>
    #include "stdafx.h"
    using namespace std;
    int main () {
        cout << "hey" << endl;
        return 0;
    }
    

    这就行了。

    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    int main () {
        cout << "hey" << endl;
        return 0;
    }
    

    Here is a great answer on what the stdafx.h header does.

        2
  •  48
  •   Hrishikesh Choudhari    15 年前

    写这段代码,它工作得很好。。

    #include "stdafx.h"
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
     cout<<"Hello World!";
      return 0;
    }
    
        3
  •  11
  •   Bo Persson Touseef    15 年前

    # include "stdafx.h"
    # include <iostream>
    using  namespace std;
    
        4
  •  10
  •   Yu Hao    11 年前

    这个 include "stdafx.h" 可以

    cout 除非你已经包括 using namespace std

    std::cout 而不是简单的 库特

        5
  •  4
  •   sth    15 年前

    我见过如果你用

    #include <iostream.h>
    

    如果你使用

    #include <iostream>  
    

    (通知-不带.h)

    那你就不会明白你提到的问题了。

        6
  •  4
  •   Jerrey    14 年前

    #include "stdafx.h" 行,放在第一位。

        7
  •  3
  •   Glen    16 年前

    下面的代码使用gcc编译并正常运行。试着复制/粘贴这个,看看它是否有效。

    #include <iostream>
    using namespace std;
    
    int bob (int a) { cout << "hey" << endl; return 0; };
    
    int main () {
        int a = 1;
        bob(a);
        return 0;
    }
    
        8
  •  3
  •   Bob Lied    16 年前

        9
  •  3
  •   Yu Hao    11 年前

        10
  •  3
  •   Sizons Joey    9 年前

    就我而言,这是一个愚蠢的解决方案:

    // Example a
    #include <iostream>    
    #include "stdafx.h"
    

    上面的代码是按照示例a编写的,当我将其更改为类似于下面的示例b时。。。

    // Example b
    #include "stdafx.h"
    #include <iostream>  
    

    我的代码编译得很有魅力。试试看,保证有效。

        11
  •  2
  •   Dan Tao    16 年前

    我有VS2010,Beta1和Beta2(一个在我的工作机器上,一个在家),我已经使用 std 很多都没有问题。尝试键入:

    std::
    

    abort , abs , acos 等等), 除了 cout ,那么, 真是个谜。在这种情况下,一定要查看C++头。

    除此之外,我只想补充一点,以确保您运行的是一个常规的、空的项目(而不是CLR,其中Intellisense是残废的),并且您确实尝试过 建造 项目至少一次。正如我在一篇评论中提到的,VS2010在添加了 include ;可能是某个东西卡住了解析器,它没有“找到” 库特

        12
  •  2
  •   sth    15 年前

    接受密码

    #include <iostream>
    using namespace std;
    

    #include "whatever your header file is named.h"
    

        13
  •  2
  •   sth    15 年前

    在从头开始一个ms c++2010项目时,我也遇到了同样的问题—我删除了ms生成的所有头文件,但使用的是:

    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    
    int main() {
       cout << "hey" << endl;
       return 0;
    }
    

    stdafx.h 因为这会导致错误。

        14
  •  2
  •   derekerdmann    13 年前

    在你开始这个程序之前,去掉所有的代码,在main里面做一个简单的hello world。仅包含iostream并使用命名空间std;。 一点一点地添加,以找到你的问题。

    cout << "hi" << endl;
    
        15
  •  1
  •   David Thornley    16 年前

    .cpp ). 检查项目设置。

    你的程序没什么问题,而且 cout namespace std . 你安装的VS2010 Beta 2是有缺陷的,我不认为这只是你的安装。

    我认为VS 2010还没有准备好C++。标准的“Hello,World”程序在Beta 1上不起作用。我试着生成了一个Win32控制台 test.cpp 文件没有 main()

        16
  •  1
  •   bluish dmajkic    13 年前

    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    
    void main( ) 
    {
       int i = 0;
       cout << "Enter a number: ";
       cin >> i;
    }
    
        17
  •  1
  •   john k    13 年前

    创建项目时,未正确设置“使用预编译头”。在属性-gt;C/C++ +gt;预编译头中更改它。

        18
  •  1
  •   Nithin    13 年前

    在visualstudio中,使用“stdafx.h”下面的所有头文件管理器。

        19
  •  1
  •   WonderWorker Sakal    9 年前

    通过在代码顶部插入以下行来包含std库:

    using namespace std;
    
        20
  •  0
  •   Manohar    16 年前

        21
  •  0
  •   avgvstvs    14 年前

    我在安装了VS2010并试图让一个几乎完全相同的程序工作之后遇到了这个错误。

    我以前在unix风格的盒子上做过普通的C代码,我决定自己来试试这个。

    我尝试的第一个程序是:

    #include "stdafx.h"
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        cout << "Hello World!";
        return 0;
    }
    

    int _tmain(int argc, _TCHAR* argv[])
    

    看起来很奇怪。它应该是:

    int main( int argc, char ** argv )
    

    在我的例子中,我只是把程序改为:

    #include <iostream>
    using namespace std;
    
    int main()
    {
         cout << "Hello world from  VS 2010!\n";
         return 0;
    }
    

    注意:使用CTRL+F5使控制台窗口保持不变,以便您可以看到结果。

        22
  •  0
  •   Paresh J    11 年前

    printf !

    stdio.h 在你的 stdafx.h 的头文件 印刷品

        23
  •  0
  •   Tenzin    9 年前

    我来这里是因为我有同样的问题 #include "stdafx.h" 它说它没有找到那个文件。
    我的诀窍是: #include <algorithm> .

    这些是你可以使用的东西,包括“计数”: Link

        24
  •  0
  •   yu yang Jian    6 年前

    stdafx.h 似乎被 pch.h 看见 this article

    #include "pch.h"
    #include <iostream>
    
    using namespace std;
    
    int main() {
        cout << "Enter 2 numbers:" << endl;
    
        25
  •  -5
  •   Wallter    16 年前

    这是一个编译器——我现在使用的是EclipseGalileo,这个程序运行起来就像一个奇迹


    推荐文章