Hi everybody. I am a beginner at programming, but the code I am trying to run is very simple and I dont know what the problem is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
#include <iostream>
#include <string>
using namespace std;
class Date {
public:
int day() {return d;}
int month() {return m;}
int year() {return y;}
int add_day();
Date(int dd, int mm, int yy):d(dd),m(mm),y(yy)
private:
int d, m , y;
};
int Date::add_day() {d = d+1;}
int main(){
Date b(21,05,1981);
cout << "b: " << b.day() << " " << b.month() << " " << b.year() << "\n";
b.add_day();
cout << "b: " << b.day() << " " << b.month() << " " << b.year() << "\n";
return 0;
}
|
I get the following error:
date.cpp: In member function ‘int Date::day()’:
date.cpp:11:21: error: ‘d’ was not declared in this scope
date.cpp: In member function ‘int Date::month()’:
date.cpp:12:23: error: ‘m’ was not declared in this scope
date.cpp: In member function ‘int Date::year()’:
date.cpp:13:22: error: ‘y’ was not declared in this scope
date.cpp: In constructor ‘Date::Date(int, int, int)’:
date.cpp:15:32: error: class ‘Date’ does not have any field named ‘d’
date.cpp:15:38: error: class ‘Date’ does not have any field named ‘m’
date.cpp:15:44: error: class ‘Date’ does not have any field named ‘y’
date.cpp:16:2: error: expected ‘{’ before ‘private’
date.cpp: In member function ‘int Date::add_day()’:
date.cpp:20:22: error: ‘d’ was not declared in this scope
However, I really cannot see what the problem is. How is it that I cannot access / see these private members d, m, or y even from within a member of the Date class? Or, for that matter, even the error that d was not declared within this scope? This seems to be the simplest case of a class I could think of, and yet it still does not work. Would appreceate some help, thanks!!!!