0

私は自分のコンストラクターを呼び出していますが、値が入る前に値を数えると正しいのですが、コンストラクター内で値を数えるとナンセンスで空白になります。

関数:

Play parse(string toParse){
    vector<string> tokens;
    string play = toParse;
    string oName, dName;
    int x, quarter, minutes, down, yardstogo, startloc, playdesc;
    for(int y=0; y<10; y++){
        x = toParse.find(",");
        tokens.push_back(toParse.substr(0,x));
        toParse = toParse.substr(x+1);
    }
    stringstream convert(tokens[1]);
    convert >> quarter;
    convert.str(tokens[2]);
    convert >> minutes;
    convert.str(tokens[6]);
    convert >> down;
    convert.str(tokens[7]);
    convert >> yardstogo;
    convert.str(tokens[8]);
    convert >> startloc;
    playdesc = findPlay(tokens[9]);
    cout << "quarter: " << quarter << endl << "oteam: " << tokens[4] << endl;
    Play a(quarter, minutes, tokens[4], tokens[5], down, yardstogo, startloc, playdesc, play);
    return a;
}

コンストラクタ:

Play::Play(int m_quarter, int m_minutes, string oTeam, string dTeam, int down, int yardToGO, int startLoc, int playDesc, string wholePlay)
{
    m_quarter = m_quarter;
    cout << m_quarter << endl;
    m_minutes = m_minutes;
    oTeam = oTeam;
    cout << oTeam << endl;
    dTeam = dTeam;
    down = down;
    yardToGO = yardToGO;
    startLoc = startLoc;
    playDesc = playDesc;
    wholePlay = wholePlay;
}

たとえば、関数では「quarter: 1 oteam: DAL」と表示されますが、コンストラクターでは「quarter: -947800344 oteam:」と表示されます。

ありがとう。

#ifndef PLAY_H_INCLUDED
#define PLAY_H_INCLUDED
#include <string>

class Play
{
private:
    int m_quarter;
    int m_minutes;
    std::string oTeam;
    std::string dTeam;
    int m_down;
    int m_yardToGO;
    int m_startLoc;
    int playDesc;
    std::string wholePlay;
public:
    int getQuarter();
    int getMinutes();
    std::string getoTeam();
    std::string getdTeam();
    int getDown();
    int getYard();
    int getStartLoc();
    int getPlayDesc();
    std::string getwholePlay();
    Play(int m_quarter, int m_minutes, std::string oTeam, std::string dTeam, int down, int yardToGO, int startLoc, int playDesc, std::string wholePlay);
    ~Play();
    Play parse(std::string toParse);
    std::string findPlay(std::string playDesc);
};

#endif // PLAY_H_INCLUDED
4

4 に答える 4

1

変数を自分自身に設定しています:

Play::Play(int m_quarter, int m_minutes, string oTeam, string dTeam, int down, int yardToGO, int startLoc, int playDesc, string wholePlay)
{
    m_quarter = m_quarter; // setting the same variable to itself
    cout << m_quarter << endl;
    m_minutes = m_minutes; // same
    oTeam = oTeam; // same
    cout << oTeam << endl;
    dTeam = dTeam; // same
    down = down; // same
    yardToGO = yardToGO; // same
    startLoc = startLoc; // same
    playDesc = playDesc; // same
    wholePlay = wholePlay; // same
}

次のようにする必要があります。

Play::Play(int quarter, int minutes, const string& offense, const string& defense, int dwn, int ytg, int start, int desc, int play) 
 : m_quarter(quarter), m_minutes(minutes), oTeam(offense), dTeam(defense), down(dwn), yardToGo(ytg), startLoc(start), playDesc(desc), wholePlay(play)
{
}

また、プレフィックスを使用してメンバー変数を示す場合は、m_一貫性を保ち、それらすべてでそれを行います。

于 2013-09-16T21:15:02.730 に答える
1

コンストラクターに問題があるようです。

メンバーの名前は、パラメーターと同じ名前であってはなりません。また、割り当ての代わりに初期化を使用します。Scott Meyer の「Effective C++」項目 12 を参照してください。

このような:

Play::Play(int quarter, int minutes, const string& offense, 
           const string& defense, int dwn, int ytg,
           int start, int desc, int play) 

         : m_quarter(quarter)
         , m_minutes(minutes)
         , oTeam(offense)
         , dTeam(defense)
         , down(dwn)
         , yardToGo(ytg)
         , startLoc(start)
         , playDesc(desc)
         , wholePlay(play)
{
}

また、それほど複雑な入力を処理する必要はありません。stringstream から読み取り、コンマをガベージ文字列変数にスローするだけです。(この形式をよく確認する必要があります。そうしないと、何らかのエラーが発生する可能性があります)

解析文字列「toParse」など:

int quarter, minutes;
string oName, dName;
int down, yardstogo, startloc, playdesc;
string wholePlay;
string comma;

stringstream ss(toParse);
toParse >> quarter >> comma >> minutes >> comma
        >> oName >> comma >> dName >> comma
        >> dwn >> comma >> yardstogo >> comma
        >> startloc >> comma >> playdesc >> comma
        >> wholePlay;

return Play(quarter, minutes, oName, dName, dwn, 
            yardstogo, startloc, playdesc, wholePlay);
于 2013-09-16T21:15:06.330 に答える
0

余談ですが、他とは違うことをしたくて、何らかの理由で Scott Meyer が気に入らないという理由でイニシャライザ リストを使用しないことを主張する場合は、thisポインタを使用できます。

Play::Play(int m_quarter,
{
    this->m_quarter = m_quarter;
    // etc
}

代わりに初期化リストを使用することをお勧めします。

于 2013-09-16T21:31:11.967 に答える