相关疑难解决方法(0)

传递'const这个参数丢弃限定符[-fpermissive]

我有一个类Cache,其函数写入指定为

bool write(const MemoryAccess &memory_access, CacheLine &cl);
Run Code Online (Sandbox Code Playgroud)

我这样称呼这个函数.

const Cache *this_cache;
c = (a==b)?my_cache:not_cache;
c->write(memory_access,cl);
Run Code Online (Sandbox Code Playgroud)

以上行给出了以下错误

"将'const Cache'作为'bool Cache :: write(const MemoryAccess&,CacheLine&)'的'this'参数传递'丢弃限定符[-fpermissive]."

这个参数是特定于编译器的,它有助于代码修改和破坏本地命名空间变量优先级.但这样的变量并没有在这里传递.

gcc g++

38
推荐指数
2
解决办法
7万
查看次数

错误:将'const ...''传递为'...'的'this'参数会丢弃限定符

stockListType.cpp:58:从这里实例化

/usr/include/c++/4.2.1/bits/stl_algo.h:91: error: passing ‘const stockType’ as ‘this’ argument of ‘bool stockType::operator<(const stockType&)’ discards qualifiers
/usr/include/c++/4.2.1/bits/stl_algo.h:92: error: passing ‘const stockType’ as ‘this’ argument of ‘bool stockType::operator<(const stockType&)’ discards qualifiers
/usr/include/c++/4.2.1/bits/stl_algo.h:94: error: passing ‘const stockType’ as ‘this’ argument of ‘bool stockType::operator<(const stockType&)’ discards qualifiers
/usr/include/c++/4.2.1/bits/stl_algo.h:98: error: passing ‘const stockType’ as ‘this’ argument of ‘bool stockType::operator<(const stockType&)’ discards qualifiers
/usr/include/c++/4.2.1/bits/stl_algo.h:100: error: passing ‘const stockType’ as ‘this’ argument of ‘bool stockType::operator<(const stockType&)’ discards qualifiers
Run Code Online (Sandbox Code Playgroud)

以上是我得到的错误,希望有人向我解释这意味着什么.我通过在重载运算符前面放置一个常量来解决错误.我的程序是一个股票市场应用程序,它读取包含字符串,5个双精度和int的文件.我们通过字符串符号和索引增益来整理程序.这本书指示我使用向量来存储每个数据.如下所示,重载运算符会比较每个符号,并使用容器的排序成员函数对其进行排序.我的问题是为什么我必须在>和<的重载运算符前放置一个常量.但不是> =,<=,==,!=重载运算符.

//function was declared in stockType.h and implemented …
Run Code Online (Sandbox Code Playgroud)

c++

14
推荐指数
1
解决办法
5万
查看次数

错误:传递const xxx,因为xxx的这个参数会丢弃限定符

我有一个问题将我的仿函数从windows移植到linux.(传递给stl :: map的仿函数用于严格弱序)原文如下:

struct stringCompare{ // Utilized as a functor for stl::map parameter for strings 
    bool operator() (string lhs, string rhs){ // Returns true if lhs < rhs
        if(_stricmp(lhs.c_str(), rhs.c_str())  < 0) return true;
        else return false;
    }
};
Run Code Online (Sandbox Code Playgroud)

由于linux不支持_stricmp但是使用strcasecmp,我将其更改为:

struct stringCompare{ 
    bool operator() (string lhs, string rhs){ // Returns true if lhs < rhs
        if(strcasecmp(lhs.c_str(), rhs.c_str())  < 0) return true;
        else return false;
    }
};
Run Code Online (Sandbox Code Playgroud)

它现在抱怨"const"参数:

passing const stringCompare as this argument of bool stringCompare::operator()  
(std::string, std::string)â discards qualifiers …
Run Code Online (Sandbox Code Playgroud)

c++ parameters compiler-errors const functor

6
推荐指数
1
解决办法
8169
查看次数

标签 统计

c++ ×2

compiler-errors ×1

const ×1

functor ×1

g++ ×1

gcc ×1

parameters ×1