Monday, February 26, 2007

C++ Namespace Issues


Problem: The program gives errors such as:
../util/ysdate.h:58: ISO C++ forbids declaration of `ostream' with no type
../util/ysdate.h:58: `ostream' is neither function nor member function; cannot be declared friend
../util/ysdate.h:58: parse error before `&' token
../util/ysdate.h:59: ISO C++ forbids declaration of `istream' with no type
../util/ysdate.h:59: `istream' is neither function nor member function; cannot be declared friend

Solution: These errors indicate that the code does not namespace qualify standard library components correctly.
Example:
int main() {
cout << "Hello world" << endl;
}

must become:
int main() {
std::cout << "Hello world" << std::endl;
}

or:
using std::cout;
using std::endl;
int main() {
cout << "Hello world" << endl;
}

or:
using namespace std;
int main() {
cout << "Hello world" << endl;
}

No comments:

Back to Top


 

Labels