构造函数存储与传入的内容不同

Constructor storing different than what's being passed in

本文关键字:存储 构造函数      更新时间:2023-10-16

我正在调用我的构造函数,但是当我在它们进入之前找出值时,它们是正确的,当我在构造函数中引用值时,我得到废话和空白。

功能:

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

您正在将变量设置为自身:

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_ 前缀表示成员变量,请保持一致并对所有成员变量进行操作。

您的构造函数似乎有一些问题。

成员的名称不应与参数同名。并使用初始化而不是赋值。见斯科特·迈耶(Scott Meyer)的"有效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);

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

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

或者更好的是,尽可能使用成员初始值设定项:

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

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

顺便说一句,如果你坚持不使用初始值设定项列表,因为你想与众不同,并且出于某种原因不喜欢 Scott Meyer,你可以使用 this 指针:

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

但我建议改用初始值设定项列表。