我的参数化构造函数说我的字符串变量未定义
My parameterized constructor says my string variable is undefined?
对不起,我很擅长编程,因此需要帮助,因此我可以继续执行其他任务。这是我下面的程序:
#include<iostream>
#include"date.h"
#include<string>
using namespace std;
int main()
{
//string thurs;
Date myDate(thurs,12,2014);
cout<<"Day is: "<<myDate.getDay()<<endl;
cout<<"month is: "<<myDate.getMonth()<<endl;
cout<<"month is: "<<myDate.getYear()<<endl;
}
在哪里说"星期四",它说它是未申报的,我试图宣布它,但仍然没有解决我的问题,这是为什么我对此进行了评论。
这是我的班级,我不确定这是否是问题:
#include<iostream>
using namespace std;
class Date
{
private:
string day;
int month;
int year;
public:
Date(string day,int month,int year); //Param Constructor
void setDay(string);
void setMonth(int);
void setYear(int);
string getDay();
int getMonth();
int getYear();
};
最后是我的固定器/Geters,不确定这是否可能是问题:
#include"date.h"
Date::Date(string day,int month, int year)
{
this->day=day;
this->month=month;
this->year=year;
}
//getters
string Date::getDay()
{
return day;
}
int Date::getMonth()
{
return month;
}
int Date::getYear()
{
return year;
}
//Setters
void Date::setDay(string d)
{
day=d;
}
void Date::setMonth(int m)
{
month=m;
}
void Date::setYear(int y)
{
year=y;
}
目前显示出" Thurs"以外的所有东西 - 任何帮助,对可怕的布局>&lt;
在C 中,必须使用双引号"
封装字符串文字,因此您可以做:
// Note "thurs" instead of thurs
Date myDate("thurs", 12, 2014);
也可以做:
string thurs = "thurs"; // Initialize a std::string with string literal "thurs"
Date myDate(thurs, 12, 2014); // Pass std::string instance
作为旁注,当您要通过不便宜的参数(例如int
,而是string
) and 时,您想制作本地副本,请考虑通过Value通过值和std::move()
,例如:
Date::Date(string d, int m, int y)
: day( std::move(d) )
, month(m)
, year(y)
{ }
void Date::setDay(string d)
{
day = std::move(d);
}
还请注意,由于Getters不会修改Date
的内部状态,因此您可能需要将它们标记为const
:
string Date::getDay() const
{
return day;
}
int Date::getMonth() const
{
return month;
}
int Date::getYear() const
{
return year;
}
尝试
Date myDate("thurs",12,2014);
请注意,帕伦斯在这里有所不同 - 从变量名称更改为字符串。
您需要定义字符串变量,例如此string thurs("literal");
line
Date myDate(thurs,12,2014);
应该是
Date myDate("thurs",12,2014);
祝你好运!
相关文章:
- 为什么我的变量没有更新,我的 LED 没有亮起?
- 为什么我的变量存在于其范围之外
- 在我的变量打印C++后无法获得 $ 打印符号
- constexpr 函数在编译时获取值,即使我的变量不是 constexpr
- 为什么我的变量在我尚未将其设置为任何值时返回一个值?
- 乘以十进制的倍数,给我一个小十进制答案,以回馈我的变量
- 我的变量将值相互混合????C++
- C++:我的简单计算器程序无法打印出来。我也认为我的变量命名不正确
- 此CMAKE错误意味着什么?我的变量设置为未发现
- 为什么 std::cout 会更改我的变量?
- 为什么我的变量在单独的线程中修改时没有改变?C++/boost
- 为什么我的变量被强制初始化为 0,即使它不应该初始化?
- 第一个C 库:我不确定在哪里参考并声明我的变量
- C++ 我是否应该完全限定我的变量类型
- 为什么当我通过引用传递数组时,我会收到一个编译器错误,指出我的变量未定义
- 为什么我的变量从1到2,然后又回到1
- 为什么重命名我的变量会阻止segfault
- 为什么我的变量被重写了
- 我的变量不会有任何值
- 如何通过C++和C#中的键盘立即按下(不按enter)将用户的数据存储在我的变量中