Menu

Cout error

Devil
2008-09-30
2012-09-26
  • Devil

    Devil - 2008-09-30

    even the simplest programs give me odd errors
    the program :

    include<iostream>

    int main()
    {
    cout<<"i rock!\n";
    }

    Error Log:
    Compiler: Default compiler
    Executing g++.exe...
    g++.exe "D:\Documents and Settings\Devil\Desktop\5.cpp" -o "D:\Documents and Settings\Devil\Desktop\5.exe" -ansi -traditional-cpp -pg -g3 -I"D:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"D:\Dev-Cpp\include\c++\3.4.2\backward" -I"D:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"D:\Dev-Cpp\include\c++\3.4.2" -I"D:\Dev-Cpp\include" -L"D:\Dev-Cpp\lib" -lgmon -pg -g3
    D:\Documents and Settings\Devil\Desktop\5.cpp: In function int main()': D:\Documents and Settings\Devil\Desktop\5.cpp:4: error:cout' undeclared (first use this function)
    D:\Documents and Settings\Devil\Desktop\5.cpp:4: error: (Each undeclared identifier is reported only once for each function it appears in.)

    Execution terminated

    any help with this will be appreciated

     
    • Mito78

      Mito78 - 2008-10-11

      I'm pretty new to C++ and just installed g++ yesterday, but I opened a new console project and added one line of code (cout<<"i rock!\n";) and it ran fine for me. The complete code was..

      include <cstdlib>

      include <iostream>

      using namespace std;

      int main(int argc, char *argv[])
      {
      cout<<"i rock!\n";
      system("PAUSE");
      return EXIT_SUCCESS;
      }

      It also ran when I used Clifford's suggestion of ..

      std::cout<<"i rock!\n";

      From your error message it looks like it can't figure out what the cout function is. Look in your include directories (after the -I in the error message)

      -I"D:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include"
      -I"D:\Dev-Cpp\include\c++\3.4.2\backward"
      -I"D:\Dev-Cpp\include\c++\3.4.2\mingw32"
      -I"D:\Dev-Cpp\include\c++\3.4.2"
      -I"D:\Dev-Cpp\include"

      and make sure there is a file called iostream.h in one of them.

      Mito

       
      • cpns

        cpns - 2008-10-12

        You are answering a question that was fully answered nearly two weeks earlier! If you read that answer carefully you will see that your suggestion is not at all the issue.

        The difference between your code and kevin_thedevil's was just the line:

        using namespace std;

        Which is solution number (3) in my earlier post. The one I described as 'least desirable'! ;-)

        If the include directories were an issue, it would have reported a pre-processor error indicating that <iostream> could not be found. That is not what the log says.

        Clifford

         
    • cpns

      cpns - 2008-09-30

      Those errors are not odd, your code is incorrect. It would have worked in GCC 2.95 but not 3.x which has stronger ISO compliance. Try the File->New->New Project->Introductory->Hello world template project to an example of similar code that does compile. Basically your code is behind teh times (by 9 years) from a language standards point of view.

      It is 'correct' with one of the following:

      1)

      include <iostream>

      int main()
      {
      std::cout<<"i rock!\n";
      }

      2)

      include <iostream>

      using std::cout ;
      int main()
      {
      std::cout<<"i rock!\n";
      }

      3)

      include <iostream>

      using namespace std ;
      int main()
      {
      std::cout<<"i rock!\n";
      }

      In this case (3) is the least desirable and most error prone solution (although I think it is the approach the aforementioned template, and many books and turorials use).

      Also note that:

      std::cout<<"i rock!" << std::endl ;

      is probably more commonly used in C++ than;

      std::cout<<"i rock!\n" ;

      Regarding the std:: namespace, ref: http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5

      Clifford

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.