相关疑难解决方法(0)

错误:将xxx作为xxx的'this'参数传递丢弃限定符

#include <iostream>
#include <set>

using namespace std;

class StudentT {

public:
    int id;
    string name;
public:
    StudentT(int _id, string _name) : id(_id), name(_name) {
    }
    int getId() {
        return id;
    }
    string getName() {
        return name;
    }
};

inline bool operator< (StudentT s1, StudentT s2) {
    return  s1.getId() < s2.getId();
}

int main() {

    set<StudentT> st;
    StudentT s1(0, "Tom");
    StudentT s2(1, "Tim");
    st.insert(s1);
    st.insert(s2);
    set<StudentT> :: iterator itr;
    for (itr = st.begin(); itr != st.end(); itr++) {
        cout << itr->getId() …
Run Code Online (Sandbox Code Playgroud)

c++

412
推荐指数
4
解决办法
38万
查看次数

g ++:const丢弃限定符

为什么我会收到discard qualifiers错误:

customExc.cpp: In member function ‘virtual const char* CustomException::what() const’:
customExc.cpp: error: passing ‘const CustomException’ as ‘this’ argument of ‘char customException::code()’ discards qualifiers
Run Code Online (Sandbox Code Playgroud)

在下面的代码示例中

#include <iostream>


class CustomException: public std::exception {

public:

    virtual const char* what() const throw() {
        static std::string msg;
        msg  = "Error: ";
        msg += code();  // <---------- this is the line with the compile error 
        return msg.c_str();
    }

    char code() { return 'F'; }
};
Run Code Online (Sandbox Code Playgroud)

在讨论类似问题之前,我已经搜索过SOF.

我已经const在每个可能的地方添加了一个.

请赐教 - 我不明白......

编辑 …

c++ const g++

8
推荐指数
1
解决办法
2万
查看次数

标签 统计

c++ ×2

const ×1

g++ ×1