ゲームをプログラムするために 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
、main()
バニーの名前を格納する正しいメモリを指し示すのに問題がある可能性があるため、出力は不規則な文字になります。mvprintw
しかし、チェーン---> troop.getname()
--->は簡単なように感じるので、理由がわかりませんbunpointer.bgetname
...