error C2065: 'cout' : undeclared identifier

前端 未结 25 2132
误落风尘
误落风尘 2020-12-01 08:40

I am working on the \'driver\' part of my programing assignment and i keep getting this absurd error:

error C2065: \'cout\' : undeclared identifier

相关标签:
25条回答
  • 2020-12-01 09:34

    write this code, it works perfectly..

    #include "stdafx.h"
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
     cout<<"Hello World!";
      return 0;
    }
    
    0 讨论(0)
  • 2020-12-01 09:34

    In Visual studio use all your header filer below "stdafx.h".

    0 讨论(0)
  • 2020-12-01 09:36

    The include "stdafx.h" is ok

    But you can't use cout unless you have included using namespace std

    If you have not included namespace std you have to write std::cout instead of simple cout

    0 讨论(0)
  • 2020-12-01 09:38

    The code below compiles and runs properly for me using gcc. Try copy/pasting this and see if it works.

    #include <iostream>
    using namespace std;
    
    int bob (int a) { cout << "hey" << endl; return 0; };
    
    int main () {
        int a = 1;
        bob(a);
        return 0;
    }
    
    0 讨论(0)
  • 2020-12-01 09:39

    In VS2017, stdafx.h seems to be replaced by pch.h see this article,

    so use:

    #include "pch.h"
    #include <iostream>
    
    using namespace std;
    
    int main() {
        cout << "Enter 2 numbers:" << endl;
    
    0 讨论(0)
  • 2020-12-01 09:40

    In Visual Studio you must #include "stdafx.h" and be the first include of the cpp file. For instance:

    These will not work.

    #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;
    }
    

    This will do.

    #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.

    0 讨论(0)
提交回复
热议问题