Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > operator<< and namespace

Reply
Thread Tools

operator<< and namespace

 
 
jalkadir
Guest
Posts: n/a
 
      10-19-2005
This program does compile, but the linker says:
main.o(.text+0x1a4):main.cpp: undefined reference to
`jme:perator<<(std: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:stream& operator<<( std:stream& os,
const jme::Name& obj );
friend std::istream& operator>>( std::istream& is,
jme::Name& obj );
};
}

--------- name.cpp
std:stream& operator<<( std: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

 
Reply With Quote
 
 
 
 
Shezan Baig
Guest
Posts: n/a
 
      10-19-2005

jalkadir wrote:
> This program does compile, but the linker says:
> main.o(.text+0x1a4):main.cpp: undefined reference to
> `jme:perator<<(std: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:stream& operator<<( std:stream& os,
> const jme::Name& obj );
> friend std::istream& operator>>( std::istream& is,
> jme::Name& obj );
> };
> }
>
> --------- name.cpp
> std:stream& operator<<( std: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-

 
Reply With Quote
 
 
 
 
jalkadir
Guest
Posts: n/a
 
      10-19-2005
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

 
Reply With Quote
 
Shezan Baig
Guest
Posts: n/a
 
      10-19-2005

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-

 
Reply With Quote
 
Al-Burak
Guest
Posts: n/a
 
      10-19-2005
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:stream& operator<<( std:stream& os,
const jme::Name& obj );
friend std::istream& operator>>( std::istream& is,
jme::Name& obj );

};
}

--------- name.cpp
std:stream& operator<<( std: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;

}

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ERROR CS0234: The type or namespace name 'DataAccessHelper' does not exist in the namespace 'BCC' (are you missing an assembly reference?) li.eddie@gmail.com ASP .Net 0 01-06-2006 11:31 AM
[XML Schema] Including a schema document with absent target namespace to a schema with specified target namespace Stanimir Stamenkov XML 3 04-25-2005 09:59 AM
Reaching into the default namespace when using another namespace. Jason Heyes C++ 1 11-19-2004 02:36 AM
Namespace: Is it a scope or a namespace? Anonymous C++ 3 08-18-2003 01:31 PM
Help:Why can't I use namespace System.Web? It is said that this namespace doesn't exist. But it should exist. Èý¹â ASP .Net 1 07-29-2003 04:31 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57