0

Sorry I'm awful at programming and need so help so I can move on with my other tasks. This is my program below:

#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; } 

Where is says "thurs" it says it is undeclared, I tried to declare it but it still didn't fix my problem, its why I commented it out.

This is my class, Im not sure if this is the problem:

#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(); }; 

and finally my Setters/Getters, not sure if this might be the problem:

#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; } 

At the moment it displays everything apart from "thurs" - any help and sorry for horrible layout >.<

1
  • 2
    thurs and not "thurs"? Commented Mar 12, 2014 at 14:06

4 Answers 4

4

In C++, string literals must be enclosed using double quotes ", so you can do:

// Note "thurs" instead of thurs Date myDate("thurs", 12, 2014); 

Or you can do:

string thurs = "thurs"; // Initialize a std::string with string literal "thurs" Date myDate(thurs, 12, 2014); // Pass std::string instance 

As a side note, when you want to pass parameters that are not cheap to copy (e.g. not int, but something like string) and you want to make a local copy, consider passing by value and std::move() from the value, e.g.:

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); } 

Note also that since the getters do not modify the internal state of Date, you may want to mark them as const:

string Date::getDay() const { return day; } int Date::getMonth() const { return month; } int Date::getYear() const { return year; } 
Sign up to request clarification or add additional context in comments.

Comments

1

Try

Date myDate("thurs",12,2014); 

Note that the parens make all the difference here - changing thurs from a variable name to a string.

1 Comment

it worked! sorry for the silly question - thankyou =]
0

You need to define the string variable like this string thurs("literal");

4 Comments

He is already using namespace std;, and "variable" is really a constant
This is not his problem actually.
imho, you should at least change "variable" to something like "literal" to avoid confusion ;)
Okay sorry for the confusion, and I thought the namespace was a detail, as he could understand it alone.
0

Line

Date myDate(thurs,12,2014); 

should be instead

Date myDate("thurs",12,2014); 

Good luck!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.