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 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-
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 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-
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;
}
======================================== This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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):
...
|
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...
|
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...
|
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,...
|
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>
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
| |