传递'const这个参数丢弃限定符[-fpermissive] [英] passing ‘const this argument discards qualifiers [-fpermissive]
问题描述
我有一个类 Cache
,它有一个函数write指定为
bool write(const MemoryAccess& memory_access,CacheLine& cl);
我这样调用这个函数。
const Cache * this_cache;
c =(a == b)?my_cache:not_cache;
c-> write(memory_access,cl);
上面的代码给了我下面的错误:
< blockquote>
传递'const Cache'作为'bool Cache :: write(const
MemoryAccess&,CacheLine&)'的'this'参数丢弃限定符[-fpermissive]。 p>
这个参数是编译器特定的,它有助于代码修改和打破局部命名空间变量优先级。但是这样一个变量并没有被传递到这里。
由于 c
是 const常量缓存*
类型的,你只能调用 const
成员函数。
您有两种选择:从申报中删除 const
$ b c
;
<2>更改
Cache :: write()$ c
bool write(const MemoryAccess& memory_access,CacheLine& cl)const;
(请注意最后添加的 const
。)
I have a class Cache
which has a function write specified as
bool write(const MemoryAccess &memory_access, CacheLine &cl);
I am calling this function like this.
const Cache *this_cache;
c = (a==b)?my_cache:not_cache;
c->write(memory_access,cl);
The above line is giving me following error
"passing ‘const Cache’ as ‘this’ argument of ‘bool Cache::write(const
MemoryAccess&, CacheLine&)’ discards qualifiers [-fpermissive]."
the this argument is compiler specific which helps in code-mangling and breaking local namespace variable priority. But such a variable is not being passed here.
解决方案 Since c
is of type const Cache *
, you can only call const
member functions on it.
You have two options:
(1) remove const
from the declaration of c
;
(2) change Cache::write()
like so:
bool write(const MemoryAccess &memory_access, CacheLine &cl) const;
(Note the added const
at the end.)
这篇关于传递'const这个参数丢弃限定符[-fpermissive]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!