我正在调用我的构造函数,但是当我在进入之前对这些值进行cout时,它们是正确的,当我在构造函数中输入值时,我会得到废话和空白。
功能:
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
答案 0 :(得分: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_
前缀表示成员变量,请保持一致并对所有变量执行此操作。
答案 1 :(得分: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);
答案 2 :(得分:0)
确保构造函数参数的名称与您的成员不同,例如
Play::Play(int quarter, //etc..
{
m_quarter = quarter;
// etc..
}
甚至更好,尽可能使用成员初始化器:
Play::Play(int quarter, //etc..
:m_quarter( quarter), // etc...
{
}
严格地说,当你执行后者时,名称可能会再次相同,但通常m_
- 前缀表示成员变量,而不是参数。
答案 3 :(得分:0)
顺便说一下,如果你坚持不使用初始化列表因为你想要与众不同而且出于某些原因不喜欢Scott Meyer,你可以使用this
指针:
Play::Play(int m_quarter,
{
this->m_quarter = m_quarter;
// etc
}
但我建议改用初始化列表。