简单的一行 class 方法返回错误值
Simple one line class method returning wrong value
我遇到了一个奇怪的问题,我不知道出了什么问题。
我有一个 class publication
由 class 成员 string headline
和 string text
.
组成
我还有一个名为 publication_and_date
的 class,它继承自 publication
,但还有一个名为 string date
的附加字段,它表示某篇文章的发表日期.
我还有一个 class news
继承自 publication_and_date
,并且有附加字段 string sources
问题是这样的:我有一个类型为 news
的对象,当我使用该对象的方法 get_date
时,我得到值 M
.
这是我的 void main:
void main()
{
news MyNews;
MyNews.set_date(3,12,2016);
MyNews.set_sources("IGN");
MyNews.set_headline("MGS V wins GOTY award");
MyNews.set_text("Metal Gear Solid V won the prestigious game of the year award!");
cout << MyNews.ToString();
getch();
}
这是class publication_and_date
:
的实现
publication_and_date::publication_and_date() : publication()
{
date="1/9/2015";
}
void publication_and_date::set_date(const int NewDay,const int NewMonth,const int NewYear)
{
if((NewDay > 31)||(NewDay < 1)||(NewMonth > 12)||(NewMonth < 1)||(NewYear<2015)) //input check
{
cout << "\nDate is invalid\n";
return;
}
date=NewDay + '/' + NewMonth + '/' + NewYear;
}
string publication_and_date::get_date()
{
return date;
}
如您所见,方法get_date()
非常简单。这只是一根线。
我不知道为什么我得到的值是 M
。
我给你的void main的输出是:
Headline: MGS V wins GOTY award
Text: Metal Gear Solid V won the prestigious game of the year award!
Date: M
Sources: IGN
我完全不明白为什么会这样。非常感谢帮助。
Edit1:这是 ToString
的代码
string news::ToString()
{
string ToReturn;
ToReturn="\nHeadline: " + get_headline() + '\n' + "Text: " + get_text() + '\n'+"Date: " + get_date()+'\n'+"Sources: " + get_sources();
return ToReturn;
}
编辑2:
我想我知道问题出在哪里了。 NewDay, NewMonth,NewYear
是整数。所以 +
运算符与字符串不同。我需要以某种方式使它们变成字符。
您得到的是 M
或其他随机数,因为您将数字相加而不是连接字符串。一个char'/'
实际上是一个小整数,值为47。
将所有内容转换为字符串的一种方法是使用 stringstream
(位于 sstream header):
std::stringstream ss;
ss << NewDay << '/' << NewMonth << '/' << NewYear;
date = ss.str();
字符串流就像您的常规 iostream,但它适用于字符串。它将在类型转换方面做正确的事情。
我遇到了一个奇怪的问题,我不知道出了什么问题。
我有一个 class publication
由 class 成员 string headline
和 string text
.
我还有一个名为 publication_and_date
的 class,它继承自 publication
,但还有一个名为 string date
的附加字段,它表示某篇文章的发表日期.
我还有一个 class news
继承自 publication_and_date
,并且有附加字段 string sources
问题是这样的:我有一个类型为 news
的对象,当我使用该对象的方法 get_date
时,我得到值 M
.
这是我的 void main:
void main()
{
news MyNews;
MyNews.set_date(3,12,2016);
MyNews.set_sources("IGN");
MyNews.set_headline("MGS V wins GOTY award");
MyNews.set_text("Metal Gear Solid V won the prestigious game of the year award!");
cout << MyNews.ToString();
getch();
}
这是class publication_and_date
:
publication_and_date::publication_and_date() : publication()
{
date="1/9/2015";
}
void publication_and_date::set_date(const int NewDay,const int NewMonth,const int NewYear)
{
if((NewDay > 31)||(NewDay < 1)||(NewMonth > 12)||(NewMonth < 1)||(NewYear<2015)) //input check
{
cout << "\nDate is invalid\n";
return;
}
date=NewDay + '/' + NewMonth + '/' + NewYear;
}
string publication_and_date::get_date()
{
return date;
}
如您所见,方法get_date()
非常简单。这只是一根线。
我不知道为什么我得到的值是 M
。
我给你的void main的输出是:
Headline: MGS V wins GOTY award
Text: Metal Gear Solid V won the prestigious game of the year award!
Date: M
Sources: IGN
我完全不明白为什么会这样。非常感谢帮助。
Edit1:这是 ToString
的代码string news::ToString()
{
string ToReturn;
ToReturn="\nHeadline: " + get_headline() + '\n' + "Text: " + get_text() + '\n'+"Date: " + get_date()+'\n'+"Sources: " + get_sources();
return ToReturn;
}
编辑2:
我想我知道问题出在哪里了。 NewDay, NewMonth,NewYear
是整数。所以 +
运算符与字符串不同。我需要以某种方式使它们变成字符。
您得到的是 M
或其他随机数,因为您将数字相加而不是连接字符串。一个char'/'
实际上是一个小整数,值为47。
将所有内容转换为字符串的一种方法是使用 stringstream
(位于 sstream header):
std::stringstream ss;
ss << NewDay << '/' << NewMonth << '/' << NewYear;
date = ss.str();
字符串流就像您的常规 iostream,但它适用于字符串。它将在类型转换方面做正确的事情。