473,396 Members | 1,989 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

operator<< and namespace

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

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

Oct 19 '05 #1
4 1407

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

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

Oct 19 '05 #2
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

Oct 19 '05 #3

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-

Oct 19 '05 #4
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::ostream& operator<<( std::ostream& os,
const jme::Name& obj );
friend std::istream& operator>>( std::istream& is,
jme::Name& obj );

};
}

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

}

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

Oct 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: franky.backeljauw | last post by:
Hello, I have a problem with using a copy constructor to convert an object of a templated class to object of another templated class. Let me first include the code (my question is below): ...
3
by: Victor Irzak | last post by:
Hello, I have an ABC. it supports: ostream & operator << I also have a derived class that supports this operator. How can I call operator << of the base class for derived object??? Is it...
1
by: Tim Partridge | last post by:
I want operator<< to be a friend function of a class inside a namespace, but I don't know how. For example: #include <iostream> namespace ns { class foo { public: // there is something...
5
by: Gianni Mariani | last post by:
Can anyone enligten me why I get the "ambiguous overload" error from the code below: friendop.cpp: In function `int main()': friendop.cpp:36: ambiguous overload for `std::basic_ostream<char,...
14
by: lutorm | last post by:
Hi everyone, I'm trying to use istream_iterators to read a file consisting of pairs of numbers. To do this, I wrote the following: #include <fstream> #include <vector> #include <iterator> ...
2
by: Harry | last post by:
Hi all, I am writing a logger program which can take any datatype. namespace recordLog { enum Debug_Level {low, midium, high}; class L { std::ofstream os; Debug_Level cdl; const...
2
by: ryan_melville | last post by:
Hi, Should I put the operator<<() for my class (which is in a namespace) in the namespace or make it global? If I understand the lookup rules correctly: If I make it global, it may be hidden...
5
by: krzysztof.konopko | last post by:
I cannot compile the code which defines a std::map type consisting of built in types and operator<< overload for std::map::value_type. See the code below - I attach a full example. Note: if I...
8
by: Goran | last post by:
Hi all, I have a question regarding operator <<. A lib of mine contains a class with an overloaded operator << as NON- class member. This would look like: #include <iostream> #include...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.