-3

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

4

1 に答える 1

1

pdcurses を使ったことはありませんが、mvprintw は printf に似ているようです。したがって、%s は、C スタイルの文字列 (const char*) を渡すことを意味しますが、std::string を指定します。std::string で c_str 関数を呼び出してみてください。

mvprintw(17,0,"%s was created!",troop.getname(1).c_str());
于 2014-05-17T19:21:33.933 に答える