Cannot access private members from within same class???

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!!!!
Unlike C#, C and C++ requires that every token be declared before it is used. You are declaring the tokens d, m, and y AFTER using them.
The problem is on line 12: you forgot { }:Date(int dd, int mm, int yy):d(dd),m(mm),y(yy) { } // Note the { }
Thank you, adding the {} did it!!! (As did using the longer {d = dd; m = mm.....} syntax )

webJose, I thought I read somewhere that within the scope of a class, it does not matter what order I declare things in? Or did I misunderstand something?
The order doesn't matter but it's weird and doesn't make sense if you do things in some orders.
The class Day has a constructor with no body so not able to compile. At least it must have blank body like { }.

Thanks
Madan
can i ask about gotoxy? because i don't have any idea. i would like to input two numbers using gotoxy.
I have seen this same construct in C# only, so I immediately diverted to "it must be one of those things" for C++. It is actually a good thing to know that within the class the order doesn't matter. I was unaware.
Topic archived. No new replies allowed.