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;
}

例如,函数说“季度:1 oteam:DAL”,而构造函数说“季度:-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)
{
}

此外,您不必处理那么复杂的输入。只需从您的字符串流中读取并将逗号放入垃圾字符串变量中即可。(这个格式一定要非常确定,否则可能会出错)

比如你的解析字符串“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 回答
0

确保您的构造函数参数的名称与您的成员不同,例如

Play::Play(int quarter, //etc..
{
    m_quarter = quarter;
    // etc..
}

或者更好的是,尽可能使用成员初始化器:

Play::Play(int quarter, //etc..
    :m_quarter( quarter), // etc...
{
}

严格来说,当你做后者时,名称可能再次相同,但通常 -m_前缀表示成员变量,而不是参数。

于 2013-09-16T21:13:46.087 回答