C ++返回std :: map的值引用

时间:2017-08-28 13:03:05

标签: c++ reference return stdmap

我有一个:std::map<string,Star> galaxy,我希望下面的find_star()方法返回对此地图中某个值的引用。我没有得到任何编译错误,但它不会返回任何引用。

Star& Galaxy::find_star(const string& name){
    try{
    return galaxy.at(name);
    }
    catch(out_of_range a){
        cerr<<"Error: "<<a.what()<<" Key not found!"<<endl;
    }
}

调试器在通过“返回”行时收到未知信号。

main.cpp
int main(){
    Galaxy g("stars-newline-leer.txt");
    g.print();
    Star s;
    s=g.find_star("Caph");//Working correctly until here 
return 0;
}

Star.cpp

Star::Star() {
}

Star::Star(const Star& obj) {
    this->id=obj.id;
    this->ms=obj.ms;
    this->prim_id=obj.prim_id;
    this->bez=obj.bez;
    this->sb=obj.sb;
    this->x=obj.x;
    this->y=obj.y;
    this->z=obj.z;


}

Star::~Star() {
}

istream& operator>>(istream& is, Star& obj) {
    string str = "";
    int i = 0;

    getline(is, str); //Id einlesen
    obj.id = stoi(str);

    getline(is, str); //Bezeichnung einlesen
    obj.bez = str;

    getline(is, str); //x-Koordinate
    obj.x = stod(str);

    getline(is, str); //y-Koordinate
    obj.y = stod(str);

    getline(is, str); //z-Koordinate
    obj.z = stod(str);

    getline(is, str); //Sternenbild
    obj.sb = str;

    getline(is, str); //Mehrfachsternsys
    obj.ms = stoi(str);

    getline(is, str); //Primärstern-Id
    obj.prim_id = stoi(str);


    return is;

}



ostream& operator<<(ostream& os, Star& obj) {
    os << "ID: " << obj.id << endl;
    os << "Name: " << obj.bez << endl;
    os << "Koordinaten: " << obj.x;
    os << ", " << obj.y;
    os << ", " << obj.z << endl;
    os << "Sternenbild: " << obj.sb << endl;
    os << "System-Id: " << obj.ms << endl;
    os << "Pimärstern: " << obj.prim_id << endl;
    return os;
}

void Star::print()const {


    cout << "ID: " << id << endl;
    cout << "Name: " << bez << endl;
    cout << "Koordinaten: " <<fixed<< x;
    cout << ", " <<fixed<< y;
    cout << ", " <<fixed<< z << endl;
    cout << "Sternenbild: " << sb << endl;
    cout << "System-Id: " << ms << endl;
    cout << "Pimärstern: " << prim_id << endl;
}

对不起我是Stackoverflow的新手,我不习惯这个。为什么我需要添加非代码来提交我的编辑。我想我只是说了一切。

1 个答案:

答案 0 :(得分:1)

您的Star函数存在设计问题,正如@CoryKramer所评论的那样。

当您获得out_of_range异常时,您不会返回map::find引用,此时您可以抛出另一个异常,但它会使您的代码变得不必复杂......

我的建议是您只需在主要功能中使用int main(){ Galaxy g("stars-newline-leer.txt"); g.print(); Star s; map<string,Star>::iterator it = s.find("Caph"); if(it != m.end()) { //element found; s = it->second; } else { cout<<"Error: "<< it->first << " Key not found!" << endl; } return 0; }

Star& Galaxy::find_star(const string& name){
    try{
      return galaxy.at(name);
    }
    catch(const std::out_of_range& a){
      cerr<<"Error: "<<a.what()<<" Key not found!"<<endl;
      throw; //an internal catch block forwards the exception to its external level
    }
}

修改

如果你想使用抛出的自定义查找函数(重新抛出异常),你可以按如下方式执行:

main

然后您需要在int main(){ Galaxy g("stars-newline-leer.txt"); g.print(); Star s; try{ s=g.find_star("Caph"); } catch(const std::exception& e){ //Do something here } return 0; } 块中再次捕获异常。

{{1}}