compile error with "cout << "Hexadecimal == 0x" << hex << m << endl;"

E

Ensoul Chee

I used
#include <iostream.h>
int m;
cout << "Hexadecimal == 0x" << hex << m << endl;

to print value of m in hexadecimal mode.

But I got the compile error like this

couttest.cpp:20 `hex' undeclared (first use this function)

What's wrong with the code?

OS: Redhat 9
Compiler: gcc 3.2.2
 
V

Victor Bazarov

Ensoul Chee said:
I used
#include <iostream.h>

Stop using this non-standard header. Its time have long been over.
Start using said:
int m;
cout << "Hexadecimal == 0x" << hex << m << endl;

'm' is used here without being initialised. That is a very bad
mistake on some systems.
to print value of m in hexadecimal mode.

'm' has indeterminate value. You can't really expect to see anything
sensible.
But I got the compile error like this

couttest.cpp:20 `hex' undeclared (first use this function)

What's wrong with the code?

Use of non-standard header. The code is a non-compilable fragment.
Perhaps you should read FAQ 5.8.
OS: Redhat 9
Compiler: gcc 3.2.2

#include <iostream>
int main()
{
int m = 42;
std::cout << "Hex of " << m << " is " \
<< std::hex << m << std::endl;
}

Victor
 
S

Sumit Rajan

Ensoul said:
I used
#include <iostream.h>
int m;
cout << "Hexadecimal == 0x" << hex << m << endl;

to print value of m in hexadecimal mode.

But I got the compile error like this

couttest.cpp:20 `hex' undeclared (first use this function)

What's wrong with the code?


The problem is that hex resides in the std namespace. So you will need to
try something like:


#include <iostream>

int main()
{
int m = 255;
std::cout << "Hexadecimal == 0x" << std::hex << m << '\n';
}

HTH,
Sumit.
 
F

Frank Schmitt

I used
#include <iostream.h>

This header is deprecated said:
int m;
cout << "Hexadecimal == 0x" << hex << m << endl;

to print value of m in hexadecimal mode.

But I got the compile error like this

couttest.cpp:20 `hex' undeclared (first use this function)

What's wrong with the code?

Everything from the standard library lives in the std:: namespace,
so you have to use the prefix std:: or an using declaration/directive.
The following example should work:

#include <iostream>

int main() {
int m = 13;
std::cout << "Hexadecimal == 0x" << std::hex << m << std::endl;
return 0;
}

HTH & kind regards
frank
 
R

Ron Natalie

Frank Schmitt said:
This header is deprecated, use #include <iostream>

It's not deprecated, it's just plain wrong. iostream.h is not
part of the standard language.
 
K

Kevin Goodsell

Ensoul said:
I used
#include <iostream.h>
int m;
cout << "Hexadecimal == 0x" << hex << m << endl;

to print value of m in hexadecimal mode.

But I got the compile error like this

couttest.cpp:20 `hex' undeclared (first use this function)

What's wrong with the code?

OS: Redhat 9
Compiler: gcc 3.2.2

In addition to the other replies, std::hex is technically presented in
<ios>. <ios> is probably #included in <iostream>, but I don't know if
this is required.

-Kevin
 
L

llewelly

Kevin Goodsell said:
In addition to the other replies, std::hex is technically presented in
<ios>. <ios> is probably #included in <iostream>, but I don't know if
this is required.

It's not. <iostream> requires several things from <ios>, but AFAIK,
it doesn't require the manipulators. So you can't rely on them
being provided by <iostream> . Really, all <iostream> provides is
cin, clog, cerr, cout, and their wide character equivalents. Lots
of other stuff comes long just becuase those objects have complex
types, which require many things, but not the manipulators.
 
E

Ensoul Chee

Thanks for your reply.

I think I need to find a good book to learn c++.

That code is copy from a book about GNU C++ for linux program.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top