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

Discussion in 'C++' started by Ensoul Chee, Sep 8, 2003.

  1. Ensoul Chee

    Ensoul Chee Guest

    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
    Ensoul Chee, Sep 8, 2003
    #1
    1. Advertising

  2. "Ensoul Chee" <> wrote...
    > I used
    > #include <iostream.h>


    Stop using this non-standard header. Its time have long been over.
    Start using <iostream>. Read about 'std' namespace as well.

    > 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
    Victor Bazarov, Sep 8, 2003
    #2
    1. Advertising

  3. Ensoul Chee

    Sumit Rajan Guest

    Ensoul Chee wrote:

    > 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.
    Sumit Rajan, Sep 8, 2003
    #3
  4. (Ensoul Chee) writes:

    > I used
    > #include <iostream.h>


    This header is deprecated, use #include <iostream>

    > 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

    --
    Frank Schmitt
    4SC AG phone: +49 89 700763-0
    e-mail: frank DOT schmitt AT 4sc DOT com
    Frank Schmitt, Sep 8, 2003
    #4
  5. Ensoul Chee

    Ron Natalie Guest

    "Frank Schmitt" <> wrote in message news:4cllsz8hpj.fsf@scxw21.4sc...
    > (Ensoul Chee) writes:
    >
    > > I used
    > > #include <iostream.h>

    >
    > 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.
    Ron Natalie, Sep 8, 2003
    #5
  6. Re: compile error with "cout << "Hexadecimal == 0x" << hex << m <<endl;"

    Ensoul Chee wrote:

    > 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
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
    Kevin Goodsell, Sep 8, 2003
    #6
  7. Ensoul Chee

    llewelly Guest

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

    Kevin Goodsell <> writes:

    > Ensoul Chee wrote:
    >
    >> 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.


    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.
    llewelly, Sep 8, 2003
    #7
  8. Ensoul Chee

    Ensoul Chee Guest

    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.
    Ensoul Chee, Sep 9, 2003
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. abi

    man cout or info cout

    abi, Jun 27, 2003, in forum: C++
    Replies:
    2
    Views:
    1,634
  2. Pmb

    std::cout vs cout

    Pmb, Jun 2, 2004, in forum: C++
    Replies:
    2
    Views:
    4,389
    Leor Zolman
    Jun 2, 2004
  3. Replies:
    4
    Views:
    308
    Howard
    Sep 15, 2005
  4. Stefan Ram

    Re: cout vs std::cout

    Stefan Ram, Sep 28, 2008, in forum: C++
    Replies:
    7
    Views:
    459
  5. Diwa
    Replies:
    6
    Views:
    2,319
    Francesco S. Carta
    Oct 2, 2009
Loading...

Share This Page