operator<< and namespace

Discussion in 'C++' started by jalkadir, Oct 19, 2005.

  1. jalkadir

    jalkadir Guest

    This program does compile, but the linker says:
    main.o(.text+0x1a4):main.cpp: undefined reference to
    `jme::eek:perator<<(std::eek:stream&, jme::Name const&)'


    here is the program's snips.

    --------- strtools.hpp
    namespace{
    calss strtools{
    std::string str;
    ........

    };
    }

    --------- name.hpp
    namespace{
    class Name : public jme::strtools{
    ....
    // This only gives you an idea as to what the f'tions do
    const std::string& getNameStr() const{return str;}
    void setName( const std::string& x){str = x;}
    void setName( const char* x){str = x;}

    friend std::eek:stream& operator<<( std::eek:stream& os,
    const jme::Name& obj );
    friend std::istream& operator>>( std::istream& is,
    jme::Name& obj );
    };
    }

    --------- name.cpp
    std::eek:stream& operator<<( std::eek:stream& os, const jme::Name& obj ) {
    return os << obj.getNameStr(); }
    std::istream& operator>>( std::istream& is, jme::Name& obj ) {
    return is >> obj.str;
    }

    --------- main.cpp
    jme::Name name("ni\xa4" "a");

    std::cout << "\"" << name << "\"" << std::endl;

    std::cout << "End of name..." << std::endl;
    std::cin.get();
    return 0;
    }

    ========================================

    what am I doing wrong?
    Does it hava anything to do with my namespace?

    TIA
     
    jalkadir, Oct 19, 2005
    #1
    1. Advertising

  2. jalkadir

    Shezan Baig Guest

    jalkadir wrote:
    > This program does compile, but the linker says:
    > main.o(.text+0x1a4):main.cpp: undefined reference to
    > `jme::eek:perator<<(std::eek:stream&, jme::Name const&)'
    >
    >
    > here is the program's snips.
    >
    > [snip]
    >
    > --------- name.hpp
    > namespace{
    > class Name : public jme::strtools{
    > ....
    > // This only gives you an idea as to what the f'tions do
    > const std::string& getNameStr() const{return str;}
    > void setName( const std::string& x){str = x;}
    > void setName( const char* x){str = x;}
    >
    > friend std::eek:stream& operator<<( std::eek:stream& os,
    > const jme::Name& obj );
    > friend std::istream& operator>>( std::istream& is,
    > jme::Name& obj );
    > };
    > }
    >
    > --------- name.cpp
    > std::eek:stream& operator<<( std::eek:stream& os, const jme::Name& obj ) {
    > return os << obj.getNameStr(); }
    > std::istream& operator>>( std::istream& is, jme::Name& obj ) {
    > return is >> obj.str;
    > }
    >
    > --------- main.cpp
    > jme::Name name("ni\xa4" "a");
    >
    > std::cout << "\"" << name << "\"" << std::endl;
    >
    > std::cout << "End of name..." << std::endl;
    > std::cin.get();
    > return 0;
    > }
    >
    > ========================================
    >
    > what am I doing wrong?
    > Does it hava anything to do with my namespace?




    Yes, namespaces without a name have a special meaning in C++ (it means
    everything inside the unnamed namespace has internal linkage). That
    means operator<< will be internal to the translation unit that it is
    defined (i.e., name.cpp) and cannot be called from any other
    translation unit.

    Remove the unnamed namespace and you should be fine.

    Hope this helps,
    -shez-
     
    Shezan Baig, Oct 19, 2005
    #2
    1. Advertising

  3. jalkadir

    jalkadir Guest

    I don't understand, what are you saying? Is it that somehow I have
    declared a unnamed space like namespace{.....}?
    The name space I use is: namespace jme{....}, what else am I supposed
    to do?
    Can you give me an example.


    TIa
     
    jalkadir, Oct 19, 2005
    #3
  4. jalkadir

    Shezan Baig Guest

    jalkadir wrote:
    > I don't understand, what are you saying? Is it that somehow I have
    > declared a unnamed space like namespace{.....}?
    > The name space I use is: namespace jme{....}, what else am I supposed
    > to do?



    Look at the code you posted. First line of name.hpp opens an unnamed
    namespace.

    -shez-
     
    Shezan Baig, Oct 19, 2005
    #4
  5. jalkadir

    Al-Burak Guest

    Correction
    I accidentally forgot to add the name of the namespace

    --------- strtools.hpp
    namespace jme{
    calss strtools{
    std::string str;
    ........

    };
    }

    --------- name.hpp
    namespace jme{
    class Name : public jme::strtools{
    ....
    // This only gives you an idea as to what the f'tions do
    const std::string& getNameStr() const{return str;}
    void setName( const std::string& x){str = x;}
    void setName( const char* x){str = x;}

    friend std::eek:stream& operator<<( std::eek:stream& os,
    const jme::Name& obj );
    friend std::istream& operator>>( std::istream& is,
    jme::Name& obj );

    };
    }

    --------- name.cpp
    std::eek:stream& operator<<( std::eek:stream& os, const jme::Name& obj ) {
    return os << obj.getNameStr(); }
    std::istream& operator>>( std::istream& is, jme::Name& obj ) {
    return is >> obj.str;

    }

    --------- main.cpp
    jme::Name name("ni\xa4" "a");

    std::cout << "\"" << name << "\"" << std::endl;

    std::cout << "End of name..." << std::endl;
    std::cin.get();
    return 0;

    }

    ========================================
     
    Al-Burak, Oct 19, 2005
    #5
    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. Èý¹â
    Replies:
    1
    Views:
    574
    William F. Robertson, Jr.
    Jul 29, 2003
  2. Replies:
    0
    Views:
    5,137
  3. Anonymous
    Replies:
    3
    Views:
    534
    Ron Natalie
    Aug 18, 2003
  4. Jason Heyes
    Replies:
    1
    Views:
    452
    Woebegone
    Nov 19, 2004
  5. mrstephengross
    Replies:
    3
    Views:
    399
    James Kanze
    May 10, 2007
Loading...

Share This Page