PDA

View Full Version : simple c++ compile problem



turbine
01-25-2005, 11:03 PM
Howdy all:

I tried to compile this very simple c++ program using gcc and/or g++.
#include <iostream> // cout, cin

main()
{
char buf[255];

cout << "Please enter a string ==> "; // O/P to STDOUT (screen).
cin >> buf; // I/P from STDIN (keyboard).
cout << "Entered string is " << buf << endl;
}

I get an error saying that cin and cout are not defined. I've tried this on both knoppix 3.3 and knoppix 3.7.

Any ideas?

markb
01-26-2005, 12:22 AM
You're using functions (cout, cin) from the standard library but you haven't told the compiler. Add the line "using namespace std;" after your include line. Or preface all your cout/cin/endl etc with std::. This is a basic C++ question, nothing to do with Knoppix etc. You code wouldn't compile on any standard compiler.

turbine
01-26-2005, 06:01 AM
Thanks for the info, markb, I'll give your suggestion a try. Even though it wouldn't compile on any standard compiler, I forgot to mention, it compiled and ran just fine with both gcc and g++ on a Debian Woody installation.