Remove this Banner Ad

simple C++ help required

🥰 Love BigFooty? Join now for free.

power09

Premiership Player
Dec 7, 2008
3,012
2,010
Burra SA
AFL Club
Port Adelaide
Other Teams
Timberwolves
Hello I am new to c++ and i cannot get rid of three errors in this project, they are highlighted in red text. I am obviously calling the functions incorrectly.

the compiler says for the first error : request for member 'precedes' in 'date', which is of none-class type 'Date*'

second error: request for member 'precedes' in 'trial', which is of none-class type 'Date*'

third error: request for member 'advance' in 'trial', which is of none-class type 'Date*'

I just dont understand what it wants me to do, This is probably very simple but as i said above C++ is new to me

I have included the the Date.h and Date.cpp files that the program uses because for all i know the problem lies there


Main.cpp

#include <cstdlib>
#include <iostream>
#include "Date.h"

using namespace std;

string Weekdays(Date* Date);
int main() {
string Event;
cout << "What date (d m y)? ";

int day,month,year;
while(cin >> day >> month >> year && getline(cin, Event)){
if(Event.length() > 0){
Event.erase(0,1);
}
if(Event.length() == 0){
Event = "this date was";
}

Date* event = new Date (day, month, year);
cout << Event;
cout << "on";
cout << Weekdays(event);
}

}

string Weekdays (Date* date) {

const string days[] = {"Sunday", "Monday", "Tuesday", "Wednesday",

"Thursday", "Friday", "Saturday"};


Date* trial = new Date(01, 01, 0001);
int weekday = 6;

if (date.precedes(trial)) {

return "Mysteryday";

} else {
while (trial.precedes(date)) {

trial.advance();

weekday = (weekday + 1) % 7;
}

return days[weekday];
}
}
Date.h
#ifndef DATE_H
#define DATE_H

class Date {
public:
int day, month , year;
Date (int d, int m, int y);
int getDay ();
void setDay (int day);
int getMonth ();
void setMonth (int month);
int getYear ();
void setYear (int year);
bool isLeapYear ();
int daysInMonth ();
void advance ();
bool precedes(Date* date);
};
#endif /* DATE_H */

Date.cpp
#include "Date.h"

Date:: Date (int& day, int& month, int& year) {
this->day = day;
this->month = month;
this->year = year;
}
int Date::getDay () {
return day;
}
void Date:: setDay (int day) {
this->day = day;
}

int Date:: getMonth () {
return this->month;
}

void Date:: setMonth (int month) {
this->month = month;
}

int Date:: getYear () {
return this->year;
}
void Date:: setYear (int year) {
this->year = year;
}
bool Date:: isLeapYear () {
if(year >= 1752){
return this->year % 400 == 0 || this->year % 100 != 0 && this->year % 4 == 0;
} else
return this->year % 4 == 0;
}
int Date :: daysInMonth () {

switch (this->month) {
case 9 :
case 4 :
case 6 :
case 11 : return 30;
default :
return 31;
case 2 :
return this->isLeapYear() ? 29 : 28;
}
}
void Date:: advance () {
this->day++;
if (this->day > this->daysInMonth()) {
this->day = 1;
this->month++;
}
if (this->month > 12) {
this->month = 1;
this->year++;
}
if(this->day == 2 && this->month == 9 && this->year == 1752)
this->day+=12;
}
bool Date:: precedes (Date date) {
return this->year < date.year
|| this->year == date.year && this->month < date.month
|| this->year == date.year && this->month == date.month && this->day < date.day;
}
 
date and trial are both pointers to an object of type 'Date'. To access the members of an object via a pointer, you must use the -> operator.

eg. date->precedes() or trial->advance()

If you are referencing an object directly (not via a pointer) you use the '.' operator. In your example this is done a number of times on the Event object which is of type string.

eg. Event.length() and Event.erase()
 

Remove this Banner Ad

simple C++ help required

🥰 Love BigFooty? Join now for free.

Back
Top