我在 C++ 中使用pdcurses来编写游戏,并且在尝试输出字符串时遇到了一些问题。
基本上相关的程序是这样的:
class Bunny {
private:
string name;
public:
string bgetname() { return name;};
}
class Troop {
private:
vector<Bunny> bunpointer; // bunpointer is a pointer to different bunnies
public:
string getname(int i) {return bunpointer[i].bgetname();};
}
/* I create some bunnies in the troop which is pointed by bunpointer
* troop is in class Troop
*/
int main() {
Troop troop; // there will be 5 bunnies in the troop at the beginning
initscr();
// .....
mvprintw(17,0,"%s was created!",troop.getname(1)); // <---- where problem is
// .....
}
该程序应该输出部队中兔子的名字,但它实际上输出了一些随机字符,如<
oru
或@
....
我的猜测是troop.getname
inmain()
可能在指向存储兔子名称的正确内存方面存在一些问题,因此输出是一些不规则字符。但我不明白为什么,因为我觉得链条mvprintw
---> troop.getname()
--->bunpointer.bgetname
很简单......