473,883 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Overloading << generates problems

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

Jul 23 '05 #1
10 1674
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
Jul 23 '05 #2
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

Jul 23 '05 #3
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


Jul 23 '05 #4
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
Jul 23 '05 #5
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
Jul 23 '05 #6

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


Jul 23 '05 #7
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
Jul 23 '05 #8
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
Jul 23 '05 #9
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
Jul 23 '05 #10

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

Similar topics

2
1298
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)
3
1332
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:
2
1318
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
3
1664
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
7
2184
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>
3
1738
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) {
4
1822
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>
8
1731
by: Micko1 | last post by:
hi there, I have an issue when overloading <<. string operator << (room * &currentPlayerLocation); string room::operator << (room * &currentPlayerLocation) {
1
1641
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,
0
9940
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9792
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11141
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, 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...
0
10742
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 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...
1
10847
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,...
0
7126
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();...
0
5991
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4220
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3232
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.