g ++ const'this'问题 [英] g++ const 'this' problems

查看:55
本文介绍了g ++ const'this'问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定是否有一个特定于G ++的新闻组...


g ++告诉我源代码与const有关的问题

而我只是没有看到它。现在,我不知道这意味着什么,但VC7

也没有看到问题:


---- snip- snip-snip ----


#include< string>

#include< set>


使用命名空间std;


class X {

class Y {

set< string> _bound;

public:

void Bind(const string& x);

};


set< Y> _requests;

public:

void ApplyChanges(const set< string>& changes);

};


void X :: Y :: Bind(const std :: string& data)

{

_bound.insert(data);

}


void X :: ApplyChanges(const set< string>& changes)

{

set< Y> :: iterator rip;

set< string> :: const_iterator ip;

for(ip = changes.begin(); ip!= changes.end (); ++ ip)

for(rip = _requests.begin(); rip!= _requests.end(); ++ rip)

rip->绑定(* ip);

}


int main(无效)

{

X x;

返回0;

}


---- snip-snip-snip ----

此源会导致以下错误消息:


test.cpp:在成员函数中`void X :: ApplyChanges(const

std :: set< std :: string,

std :: less< std :: string>,std :: allocator< std :: string>>&)'' :

test.cpp:30:传递`const X :: Y''作为`this''的参数`void

X :: Y :: Bind(const

std :: string&)''丢弃限定符


现在,我不能为我的生活看到我是如何违反const的规则

。我没有传递一个const''this''指针。我正在传递一个const

参数,是的,但它不是这个指针。


有什么想法吗?

John

解决方案

John Calcote写道:

我不确定是否有g ++特定的新闻组...

g ++告诉我,我的源代码与const有关的问题
我只是没有看到它。现在,我不知道这意味着什么,但VC7
也没有看到问题:

---- snip-snip-snip ----

#include< string>
#include< set>

使用命名空间std;

class X {
Y类{
设置< string> _bound;
public:
void Bind(const string& x);
};

set< Y> _requests;
public:
void ApplyChanges(const set< string>& changes);
};

void X :: Y :: Bind(const std :: string& data)
{
_bound.insert(data);
}

void X :: ApplyChanges(const set< string>& changes )
{
设置< Y> :: iterator rip;
设置< string> :: const_iterator ip;
for(ip = changes.begin(); ip!= changes .end(); ++ ip)
for(rip = _requests.begin(); rip!= _requests.end(); ++ rip)
rip-> Bind(* ip);


我将代码更改为:


Y * pr_rip =&(* rip);

pr_rip->绑定(* ip);


gcc_constthis.cpp:30:错误:无效转换为`const X :: Y *''到

`X :: Y *''


看来虽然它是一个const_iterator。


看起来像一个gcc bug。


它在VC ++ 7.1上编译得很好。

}

int main(无效)
{
X x;
返回0;
}
---- snip-snip-snip ----
此源会导致以下错误消息:

test.cpp:在成员函数`void X :: ApplyChanges(const
std :: set< std :: string,
std :: less< std: :string>,std :: allocator< std :: string>>&)'':
test.cpp:30:将`const X :: Y''作为`this'的参数传递给`void
X :: Y :: Bind(const
std :: string&)''丢弃限定词

现在,我不能为我的生活看我怎么样我违反了常规les
在这里。我没有传递一个const''this''指针。我正在传递一个const
参数,是的,但它不是这个指针。

任何想法?
John




John Calcote写道:

我不确定是否有特定于新闻组的g ++ ...


有:news:gnu.g ++。help


[snip]

[其他所有处理elsethread。]


干杯,

- g

-

Artie Gold - 德克萨斯州奥斯汀

哦,对于普通老垃圾邮件的美好时光。


2003年12月19日14:27:10 -0800 , jc******@novell.com (John Calcote)

写道:

我不确定是否有g ++特定的新闻组...

g ++告诉我这里有一个与const有关的问题我的源代码
我只是没有看到它。现在,我不知道这意味着什么,但VC7
也没有看到问题:




看来GCC领先于游戏,已经实施了这个

缺陷报告:
http://std.dkuug.dk/jtc1/sc22/wg21/d...fects.html#103

Tom


C ++ FAQ: http://www.parashift.com/c++-faq-lite/

C常见问题: http://www.eskimo.com/~scs/C-faq/top.html


I''m not sure is there''s a g++ specific newsgroup...

g++ is telling me there''s a const-related problem with my source code
and I just don''t see it. Now, I don''t know that it means much, but VC7
doesn''t see the problem either:

----snip-snip-snip----

#include <string>
#include <set>

using namespace std;

class X {
class Y {
set<string> _bound;
public:
void Bind(const string & x);
};

set<Y> _requests;
public:
void ApplyChanges(const set<string> & changes);
};

void X::Y::Bind(const std::string & data)
{
_bound.insert(data);
}

void X::ApplyChanges(const set<string> & changes)
{
set<Y>::iterator rip;
set<string>::const_iterator ip;
for (ip = changes.begin(); ip != changes.end(); ++ip)
for (rip = _requests.begin(); rip != _requests.end(); ++rip)
rip->Bind(*ip);
}

int main(void)
{
X x;
return 0;
}

----snip-snip-snip----

This source causes the following error messages:

test.cpp: In member function `void X::ApplyChanges(const
std::set<std::string,
std::less<std::string>, std::allocator<std::string> >&)'':
test.cpp:30: passing `const X::Y'' as `this'' argument of `void
X::Y::Bind(const
std::string&)'' discards qualifiers

Now, I can''t for the life of me see how I''m violating const rules
here. I''m not passing a const ''this'' pointer. I''m passing a const
parameter, true, but it''s not the this pointer.

Any ideas?
John

解决方案

John Calcote wrote:

I''m not sure is there''s a g++ specific newsgroup...

g++ is telling me there''s a const-related problem with my source code
and I just don''t see it. Now, I don''t know that it means much, but VC7
doesn''t see the problem either:

----snip-snip-snip----

#include <string>
#include <set>

using namespace std;

class X {
class Y {
set<string> _bound;
public:
void Bind(const string & x);
};

set<Y> _requests;
public:
void ApplyChanges(const set<string> & changes);
};

void X::Y::Bind(const std::string & data)
{
_bound.insert(data);
}

void X::ApplyChanges(const set<string> & changes)
{
set<Y>::iterator rip;
set<string>::const_iterator ip;
for (ip = changes.begin(); ip != changes.end(); ++ip)
for (rip = _requests.begin(); rip != _requests.end(); ++rip)
rip->Bind(*ip);
I changed that code to this:

Y * pr_rip = &(* rip);
pr_rip->Bind(*ip);

gcc_constthis.cpp:30: error: invalid conversion from `const X::Y*'' to
`X::Y*''

It seems at though it is a const_iterator.

Looks like a gcc bug.

It compiles fine on VC++7.1.
}

int main(void)
{
X x;
return 0;
}

----snip-snip-snip----

This source causes the following error messages:

test.cpp: In member function `void X::ApplyChanges(const
std::set<std::string,
std::less<std::string>, std::allocator<std::string> >&)'':
test.cpp:30: passing `const X::Y'' as `this'' argument of `void
X::Y::Bind(const
std::string&)'' discards qualifiers

Now, I can''t for the life of me see how I''m violating const rules
here. I''m not passing a const ''this'' pointer. I''m passing a const
parameter, true, but it''s not the this pointer.

Any ideas?
John




John Calcote wrote:

I''m not sure is there''s a g++ specific newsgroup...


There is: news:gnu.g++.help

[snip]
[Everything else dealt with elsethread.]

Cheers,
--ag

--
Artie Gold -- Austin, Texas
Oh, for the good old days of regular old SPAM.


On 19 Dec 2003 14:27:10 -0800, jc******@novell.com (John Calcote)
wrote:

I''m not sure is there''s a g++ specific newsgroup...

g++ is telling me there''s a const-related problem with my source code
and I just don''t see it. Now, I don''t know that it means much, but VC7
doesn''t see the problem either:



It seems GCC is ahead of the game, already having implemented this
defect report:
http://std.dkuug.dk/jtc1/sc22/wg21/d...fects.html#103

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html


这篇关于g ++ const'this'问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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