重载<<产生问题 [英] Overloading << generates problems

查看:58
本文介绍了重载<<产生问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我有以下代码:

班级考试{

public:

test(const std :: string * n):name(n){}

virtual~test(){}


const std: :string * getName(){返回名称; }


受保护:

const std :: string * name; ///<变量名。


私人:

};


std :: ostream& operator<<(std :: ostream& s,const test& v){

return s<< v.getName();

}


当我尝试编译时我得到:

common / test.cc:在函数`std :: ostream& operator<<(std :: ostream&,

const test&)'':

common / test.cc:5:错误:没有匹配函数用于调用`test

:: getName()const''

common / test.h:14:错误:候选人是:const std :: string *

test :: getName()< near match>

make:*** [test.o]错误1


任何关于哪里的想法问题是什么?


干杯,


保罗马托斯

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

推荐答案

pmatos schrieb:
pmatos schrieb:
大家好,

我有以下代码:
class test {
public:
test(const std :: string * n):name(n){}
virtual ~test(){}
const std :: string * getName(){返回名称; }
const std :: string * getName()const {return name; }
protected:
const std :: string * name; ///<变量名。

私人:
};

std :: ostream& operator<<(std :: ostream& s,const test& v){
return s<< v.getName();
}

当我尝试编译时我得到:
common / test.cc:在函数`std :: ostream& operator<<(std :: ostream&,
const test&)'':
common / test.cc:5:错误:没有匹配函数来调用`test
:: getName()const''
common / test.h:14:错误:候选者是:const std :: string *
test :: getName()< near match>
make: *** [test.o]错误1

关于问题所在的任何想法?
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是一个const测试& ;,所以测试:: getName()需要是const(无论如何都应该是

,因为它不会修改*这个)。除此之外,你确定关于指针的价值是多少?难道这些字符串不应该是引用吗?

值适当的值?


干杯,

Malte



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


问题是你用const对象调用一个函数getName()
但是const的声明不是const中的那个

时尚可能。尝试将关键字放在最后,这将是

解决当前问题。


-

谢谢

Shabbir Bhimani
http://www.go4expert.com

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写道:
shabbir wrote:
问题是你用const对象调用函数getName()但是声明const的概率并不恰好是const。尝试把关键字放在最后,这将解决当前的问题。


谢谢大家,


但是,这个解决方案通过运营商强制执行<<重载

我无法访问类私有部分。因为我超载了

运营商<<仅出于调试目的,我希望能够打印

他们所有人都没有getter。这个

情况下最好的解决方案是什么?使用print()方法然后从

运算符调用print()<<超载?


干杯,


保罗马托斯

-
谢谢
Shabbir Bhimani
http://www.go4expert.com
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






这篇关于重载&lt;&lt;产生问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆