473,396 Members | 1,975 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.

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 1597
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
I know this isn't what you were asking, but your code makes me
nervious. Where does the memory for test::name get created? Is it on
the stack? or on the heap? Is the test class responsible for deleting
it or not? What about the default copy constructor that the compiler
generates, it's a shallow copy, is that what you want?...

I realize that this class may be used only for illustration purposes,
but IMHO it has a lot of potential problems.

Jul 23 '05 #11

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

Similar topics

2
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
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...
2
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...
3
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....
7
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,...
3
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...
4
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
by: Micko1 | last post by:
hi there, I have an issue when overloading <<. string operator << (room * &currentPlayerLocation); string room::operator << (room * &currentPlayerLocation) {
1
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
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...
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
agi2029
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.