gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 05:01:11 31 4
gpt4 key购买 nike

<分区>

我有一个: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 的新手,我不习惯这个。为什么我需要添加非代码来提交我的编辑。我想我只是说了一切。

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com