| woodchuck (7) | |||
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:
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!!!! | |||
|
|
|||
| webJose (2948) | |
| 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. | |
|
|
|
| coder777 (2550) | |
The problem is on line 12: you forgot { }:Date(int dd, int mm, int yy):d(dd),m(mm),y(yy) { } // Note the { }
| |
|
|
|
| woodchuck (7) | |
|
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? | |
|
|
|
| L B (3816) | |
| The order doesn't matter but it's weird and doesn't make sense if you do things in some orders. | |
|
|
|
| mbisht (4) | |
|
The class Day has a constructor with no body so not able to compile. At least it must have blank body like { }. Thanks Madan | |
|
|
|
| acDz (9) | |
| can i ask about gotoxy? because i don't have any idea. i would like to input two numbers using gotoxy. | |
|
|
|
| webJose (2948) | |
| 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. | |
|
|
|