PDA

View Full Version : Unusual Problem



gnupzv
03-12-2005, 08:29 PM
Here is my simple code. The problem occured in a much more difficult program, but this is the simplest example that I could generate.



// test.cpp
#include<iostream>
main()
{
cout << "test\n";
}


I am using the gnu g++ compiler under linux Mandrake 7.2. I used the following command to compile: g++ test.cpp

The following error occured:

test.cpp: In function `int main()':
test.cpp:5: `cout' undeclared (first use this function)
test.cpp:5: (Each undeclared identifier is reported only once for each function it appears in.)

My first thought was that the header file was not within my path, but it was indeed there. I switched over to the <iostream.h> but I got a compiler warning about out of date header files that I was able to counteract by using the appropriate switch. This solution, does not lend itself to using the STL headers. Since I end up with a similar error for the STL functions and type definitions. Is there a compiler switch that needs to be used to use the up-to-date header files?

jverkoey
03-12-2005, 08:33 PM
using namespace std;

Must be in there before the main function.

Also, main should be int main

And this belongs in the C++ forum.

MadCow257
03-12-2005, 08:34 PM
The error is easy to fix.


#include<iostream>

using std::cout;
//alternatively
//using namespace std;

int main()
{
cout << "test\n";
}