Hi all,
I have the following code:
class test {
public:
test(const std::string *n) : name(n) {}
virtual ~test() {}
const std::string * getName() { return name; }
protected:
const std::string * name; ///< Variable name.
private:
};
std::ostream& operator<<(std: :ostream& s, const test& v) {
return s << v.getName();
}
And I get when I try to compile:
common/test.cc: In function `std::ostream& operator<<(std: :ostream&,
const test&)':
common/test.cc:5: error: no matching function for call to `test
::getName() const'
common/test.h:14: error: candidates are: const std::string*
test::getName() <near match>
make: *** [test.o] Error 1
Any ideas on where the problem is?
Cheers,
Paulo Matos 10 1677
pmatos schrieb: Hi all,
I have the following code: class test { public: test(const std::string *n) : name(n) {} virtual ~test() {}
const std::string * getName() { return name; }
const std::string * getName() const { return name; } protected: const std::string * name; ///< Variable name.
private: };
std::ostream& operator<<(std: :ostream& s, const test& v) { return s << v.getName(); }
And I get when I try to compile: common/test.cc: In function `std::ostream& operator<<(std: :ostream&, const test&)': common/test.cc:5: error: no matching function for call to `test ::getName() const' common/test.h:14: error: candidates are: const std::string* test::getName() <near match> make: *** [test.o] Error 1
Any ideas on where the problem is?
v is a const test&, so test::getName() needs to be const (should be
anyway, as it doesn't modify *this). Apart from that, are you sure
about the pointers? Shouldn't all those strings be references and
values where appropriate?
Cheers,
Malte
The problem is you are calling a function getName() with const object
but the declaration of const does not happen to be const in that
fashion probably. Try putting the keyword at the last and that will
solve the current problem.
--
Thanks
Shabbir Bhimani http://www.go4expert.com
shabbir wrote: The problem is you are calling a function getName() with const object but the declaration of const does not happen to be const in that fashion probably. Try putting the keyword at the last and that will solve the current problem.
Thanks all,
However, this solution enforces that through the operator<< overloading
I cannot access the class private parts. Since I'm overloading
operator<< for debuggins purposes only I would like to be able to print
them without getters for all of them. What's the best solution in this
situation? Having a print() method and then calling print() from the
operator<< overloading?
Cheers,
Paulo Matos
-- Thanks Shabbir Bhimani http://www.go4expert.com
pmatos schrieb: Thanks all,
However, this solution enforces that through the operator<< overloading I cannot access the class private parts. Since I'm overloading operator<< for debuggins purposes only I would like to be able to print them without getters for all of them. What's the best solution in this situation? Having a print() method and then calling print() from the operator<< overloading?
Either that or the usual way employing friendship. (Below with an
inline operator for brevity. If it's more complex than that, the
definition should be elsewhere):
class Foo
{
public:
Foo( const std::string& name )
: m_name( name ) {}
const std::string& name() const
{ return m_name; }
private:
std::string m_name;
friend void operator <<( std::ostream& s, const Foo& v )
{
return s << v.m_name;
}
};
Cheers,
Malte
pmatos wrote: shabbir wrote: The problem is you are calling a function getName() with const object but the declaration of const does not happen to be const in that fashion probably. Try putting the keyword at the last and that will solve the current problem.
Thanks all,
However, this solution enforces that through the operator<< overloading I cannot access the class private parts. Since I'm overloading operator<< for debuggins purposes only I would like to be able to print them without getters for all of them. What's the best solution in this situation? Having a print() method and then calling print() from the operator<< overloading?
make the operator<< a friend of the class:
class test {
friend std::ostream& operator<<(std: :ostream& s, const test& v);
public:
test(const std::string *n) : name(n) {}
virtual ~test() {}
const std::string * getName() const { return name; }
protected:
const std::string * name; ///< Variable name.
private:
};
--
Karl Heinz Buchegger kb******@gascad .at
Malte Starostik wrote: class Foo { public: Foo( const std::string& name ) : m_name( name ) {} const std::string& name() const { return m_name; }
private: std::string m_name; friend void operator <<( std::ostream& s, const Foo& v ) { return s << v.m_name; }
Should return std::ostream& instead of void, right?
Should it be declared private?
Paulo Matos
};
Cheers, Malte I have the following code: class test { public: test(const std::string *n) : name(n) {} virtual ~test() {}
const std::string * getName() { return name; }
protected: const std::string * name; ///< Variable name.
private: };
std::ostream& operator<<(std: :ostream& s, const test& v) { return s << v.getName(); }
What about to make operator<< a friend of the test class:
class test {
....
friend std::ostream & operator<<( std::ostream & s, const test & v );
};
std::ostream& operator<<(std: :ostream& s, const test& v) {
return s << v.name;
}
-- Marek
pmatos wrote: Malte Starostik wrote:
class Foo { public: Foo( const std::string& name ) : m_name( name ) {} const std::string& name() const { return m_name; }
private: std::string m_name; friend void operator <<( std::ostream& s, const Foo& v ) { return s << v.m_name; } Should return std::ostream& instead of void, right?
right
Should it be declared private?
Why?
Don't you think it is a good idea if some code just can do:
int main()
{
Foo test;
cout << test << '\n';
}
No harm is done to the 'test' object by that. So if someone wants to
output it, well, let him do so!
--
Karl Heinz Buchegger kb******@gascad .at
pmatos schrieb: Malte Starostik wrote:
class Foo { public: Foo( const std::string& name ) : m_name( name ) {} const std::string& name() const { return m_name; }
private: std::string m_name; friend void operator <<( std::ostream& s, const Foo& v ) { return s << v.m_name; }
Should return std::ostream& instead of void, right? Should it be declared private?
Sorry, of course it should return std::ostream&
It's not private, it's a free-standing inline friend function. More
verbose alternative, non-inline:
class Foo
{
//...
friend std::ostream& operator <<( std::ostream&, const Foo& );
};
std::ostream& operator <<( std::ostream& s, const Foo& v )
{
//...
}
Cheers,
Malte This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Tatu Portin |
last post by:
1: #include <iostream>
2:
3: typedef struct {
4: double r;
5: double i;
6: } complex;
..
..
..
24: ostream & operator<< (ostream &str, const complex &a)
|
by: pmatos |
last post by:
Hi all,
I'm having a problem and for illustration purposes I developed code
that shows what the problem is about. However, any comment on the code
which is not directly about this issue is surely welcome. I have 3
classes, A is abstract, B and C inherit from A and then I create a
vector with B's and C's and print them. Of course, I overload << for
each one of them.
So it is as follows:
|
by: zeroYouMustNotSpamtype |
last post by:
Hi,
Describing this problem will be a bit long winded, but please bear
with me:
I've got three files in my project: permuts.h, permuts.cpp, and
braids.cpp (some content from wich will eventually be moved into
braids.h). Both cpp files include the h file. Permuts.h contains the
Permutation class, for which I needed to overload <<. Doing this in
that file caused an error(Duplicate definition I think - it occurs to
|
by: Suresh Tri |
last post by:
Hi all,
I was trying to overload '<' operator for (varchar,varchar). But in
the function which handles the comparision I want to use the previous
'<' operator.. but it is going into a recursion.
My simplified code looks like :
create or replace function orastringcmp (varchar, varchar) returns
boolean as
'declare
|
by: Ook |
last post by:
The following code compiles and runs. In my overloaded operator<<, I
call Parent.stuff. I would expect it to call Child.stuff, since Child
is the child class, but it does not. What am I missing, and how can I
get it to call Child.stuff. Eventually I'll have different child
classes, and I need it to call stuff() from the associated child
class, not Parent.stuff.
#include <iostream>
#include <ostream>
#include <string>
| |
by: johnmmcparland |
last post by:
Hi all,
I know it is possible to overload the operators and < in C++ but how
can I do this.
Assume I have a class Date with three int members, m_day, m_month and
m_year. In the .cpp files I have defined the < and operators;
bool Date::operator<(const Date& d)
{
|
by: nomad5000 |
last post by:
Hello!
I'm trying to overload the << operator but it just won't work
my code is the following:
the student.h file
#include <string>
|
by: Micko1 |
last post by:
hi there,
I have an issue when overloading <<.
string operator << (room * ¤tPlayerLocation);
string room::operator << (room * ¤tPlayerLocation)
{
|
by: xkenneth |
last post by:
Hi,
I'm writing a sparse matrix class for class and I cannot seem to get
operator overloading to work properly.
I've overloaded an operator with the code here.
matrix operator +(matrix one, matrix other) {
cout << "adding" << endl;
return matrix(NULL,2,2); //doesn't actually carry out the operation,
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |