operator<< and namespace

J

jalkadir

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
 
S

Shezan Baig

jalkadir said:
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-
 
J

jalkadir

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
 
S

Shezan Baig

jalkadir said:
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-
 
A

Al-Burak

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;

}

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

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

No members online now.

Forum statistics

Threads
473,955
Messages
2,570,117
Members
46,705
Latest member
v_darius

Latest Threads

Top