Expected primary expression before...

Hello, this is my first post on this forum so I hope this will make sense (I already tried searching the site for people having similar problems but I didn't see any that seemed to match my problem):

I need a little help with my homework :P
I am using Dev C++ environment to compile my program. I've gotten my program down to 6 (identical) errors. I am trying to write a method to check whether two dates are the same.
I have the class "date" declared in a .h file and I am defining them in a seperate .cpp file.
I've #included date.h in both date.cpp and main.cpp and I've tried just moving the code from date.h into date.cpp and got rid of the #includes but it still gives me the same "expected primary-experession before '.' token" error when I try to compile.

Here is the section of code where it is giving me the errors:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 bool Date::sameDate(Date otherDate)
{
     if (otherDate.month == Date.month &&
         otherDate.day == Date.day &&
         otherDate.year == Date.year)
     {
         return true;
     }
     
     if (otherDate.month != Date.month ||
         otherDate.day != Date.day ||
         otherDate.year != Date.year)
     {
         return false;
     }
} 


Is it because I am using the abstract data type as an argument to the method while I'm defining said abstract data type?
You are using Date.month, etc. when I presume you are meaning effectively this->month, or simply month to refer to the calling object.
Hullo, trPTemper, welcome to the forum!

Instead of a whole 'nother if statement, just use an else. Or even simpler, like this:
1
2
3
4
5
6
bool Date::sameDate(Date otherDate)
{
        if(all are the same)
              return true;
        return false;
}


Hope this helps!

EDIT: Yes, and what Zhuge said.
Last edited on
Thanks alot! I changed Date.x to this->x and it compiles and runs perfectly. thanks for the tip Superdude! Your way is much simpler and easier to read. Hopefully I'll think of that in the future and save myself some typing :P
You actually don't need to use this-> unless you have a local variable named the same as it.
Topic archived. No new replies allowed.