构造函数存储与传入的内容不同
Constructor storing different than what's being passed in
我正在调用我的构造函数,但是当我在它们进入之前找出值时,它们是正确的,当我在构造函数中引用值时,我得到废话和空白。
功能:
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
}
但我建议改用初始值设定项列表。
相关文章:
- 我可以在类中存储构造函数的模板参数吗?
- 为什么std::atomic的默认构造函数不默认初始化底层存储值
- 如何将 std::string 作为构造函数参数传递,并将其保存的 C 字符串存储在 void 指针中?
- 为同一存储位置调用构造函数两次是否合法?
- 自动存储中没有无参数构造函数的类对象和异常
- 在类中存储 Typedef 构造函数
- 如何将用户输入存储在变量中,该变量与默认构造函数中的变量初始化中的值不同
- 线程本地存储构造函数 g++
- 可视化 C++构造函数的存储位置
- 如何在std::vector中存储不带复制或移动构造函数的对象
- 为构造函数中的结构数组分配存储C++
- 具有可变参数构造函数的类模板;将参数存储到 std::vector< std::shared_ptr<> >
- 构造函数存储与传入的内容不同
- 复制构造函数:释放存储时
- c++将一个对象存储到该对象的构造函数内的对象数组中
- 通过 <vector> 删除复制构造函数等,按值(无指针)将具有相同基类的对象存储在C++中
- 为存储库构造函数分配内存
- 存储 JavaScript 构造函数供以后使用
- 如何为在 std::map 中存储指针的模板类编写复制构造函数
- 如何在类构造函数中存储实例引用