Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;指向从未设置为常量的常量对象的指针_C++_Pointers_Constants - Fatal编程技术网

C++ C++;指向从未设置为常量的常量对象的指针

C++ C++;指向从未设置为常量的常量对象的指针,c++,pointers,constants,C++,Pointers,Constants,我有一组学生和一个迭代器,迭代器找到一个特定的学生,然后我需要更改。问题是,当我去改变指针指向的对象时,它说这个对象是常量。我不确定这是为什么,因为我不认为我曾经明确地使对象为常量。我对C++比较陌生,所以我可能会做一些意外的事情。 这是主要功能 set<Student> students; ifstream file(*somefilename*); while (!file.is_open()) { cout << filename << endl

我有一组学生和一个迭代器,迭代器找到一个特定的学生,然后我需要更改。问题是,当我去改变指针指向的对象时,它说这个对象是常量。我不确定这是为什么,因为我不认为我曾经明确地使对象为常量。我对C++比较陌生,所以我可能会做一些意外的事情。 这是主要功能

set<Student> students;
ifstream file(*somefilename*);
while (!file.is_open())
{
    cout << filename << endl;
    cout << "Could not open file. Enter new filename: ";
    cin >> filename;
    file.open(filename);
}

while (!file.eof()) {
    string temp = "";
    string name;
    int regNo;
    if (file.eof())break;
    for (int i = 0; i < 3; i++) {
        if (i == 0)
            file >> regNo;
        else {
            file >> temp;
            name += temp;
        }
    }
    cout << "For loop done" << endl;
    students.insert(Student(name, regNo));
}

file.close();

file.open("ex1/marks.txt");

while(!file.eof()){
    int regNo;
    string module;
    int mark;
    file >> regNo;
    Student tempStud("",regNo);
    file >> module;
    file >> mark;
    set<Student>::iterator it = students.find(tempStud);
    if (it != students.end()) {
        **it->addMark(module, mark);**//here's the problem code
    }
}

file.close();

for (set<Student>::iterator it = students.begin(); it != students.end(); it++)
    cout << *it << endl;

cin.get();}
set学生;
ifstream文件(*somefilename*);
而(!file.is_open())
{
cout>regNo;
否则{
文件>>临时文件;
姓名+=临时;
}
}
cout regNo;
学生临时职位(“,注册号);
文件>>模块;
文件>>标记;
set::iterator it=students.find(tempstrud);
if(it!=students.end()){
**它->添加标记(模块,标记);**//这是问题代码
}
}
file.close();
for(set::iterator it=students.begin();it!=students.end();it++)

cout您没有做任何事情来意外地使Student对象
const


问题 这是你的问题:()

集合中的所有迭代器都指向常量元素

换句话说,
set
将不允许您通过迭代器修改元素,即使元素在技术上不是
const

那么为什么
set
是这样定义的呢?()

集合的元素将按排序顺序排列。如果允许的话 修改一个元素,则无法维护此排序顺序。 因此,您不能修改该项


解决方案 你有三个选择之一

1) 不要使用
set
除非有非常好(而且非常具体)的理由需要使用
set
,否则不要这样做。改用。它不会有相同的限制

2) 拆下并插入 删除旧元素并插入该元素的“更新”版本。这需要O(1)个时间。()

3) 使用
mutable

如果要修改的数据成员不涉及对象类型的自然顺序,则将它们声明为
可变的
。这相当于说它们的值不会改变类的逻辑
常量
实例的标识。这将允许您甚至在
const
对象或迭代器()中变异这些数据成员,错误发生在哪一行?错误消息到底说了什么?(引用它)另外,请确保张贴一个例子,上面说什么对象是常数?。其中的MCVE似乎是修改
std::set
、声明可变元素的最佳方式。为什么不在提交之前写一个完整的答案?@AustinBrunkhorst Point#6我特别指的是在部分状态下发布答案。这个链接是正确的,但是我不相信它指的是你的流媒体编辑方式,因为你实际上是在输入它们。我以为是这样的,但我找不到任何可以证实这一点的东西。非常感谢你!我最终选择了你建议的第二个选项,而且效果很好。
    public:
       Student(const string &name, int regNo);

    int getRegNo() const;

    void addMark(string& module, float mark);

    float getMark(const string &module) const;

    float getMin() const;

    float getMax() const;

    float getAvg() const;

    bool operator <(const Student& s2) const;

    bool operator >(const Student& s2);

    bool operator ==(const Student& s2);

private:
    int regNo;
    map<string, float> marks;  // keys are modules, values are marks in range 0.0 to 100.0
friend ostream& operator<<(ostream &str, const Student &s);