Help with C++ inheritance and composition

Cannot get rid of 4 errors

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

33 Replies - 1833 Views - Last Post: 30 March 2010 - 04:06 AM Rate Topic: -----

#1 David Lee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 21-March 10

Help with C++ inheritance and composition

Posted 30 March 2010 - 01:38 AM

Create a new project which consists of three classes; an Event class, the Time class and a Date class. I have 4 errors that I cannot get rid of. I have been trying for hours, but everytime I alter the code, I get more errors. Can someone help me?
#include <string>
#include <iostream>

using namespace std;



class Time
{

public:
        void setTime(int hour, int minute);
        void getTime(int&hr, int&min);
        void printTime() const;
        Time(int hour, int minute);
        Time();
private:
        int hour;
        int minute;
};

class Date
{
public: 
        Date(int month, int day, int year);
        Date();
        void getDate(int&mm, int&dd, int&yy);
        void setDate(int month = 1, int day = 1, int year = 1900);
        void printDate();
private:
        int month;
        int day;
        int year;
};

class Event
{
public:
        void setEventData(string eventName; int hour, int minute, int month, int day, int year);
        void printEventData();
        Event (string eventName= "Event", int hour = 0, int minute = 0,int month = 1, int day = 1, int year = 1900);            

private:
        
        Time eventTime;
        Date eventDate;
        string eventName;

};

Time::Time(){hour= minute = 0;} 
Time::Time (int hr, int min) 
{
	hour = hr; minute = min;
}
void Time::getTime( int &hr, int &min) { hr = hour; min = minute;}
void Time::setTime( int hr, int min) { hour = hr; minute = min;}
void Time::printTime()
{

        int hr, int min;
        getTime (hr, min);
        if (hr < 20) cout << "0";
        cout << hr << ":";
        if (min < 20) cout << "0";
}
Date::Date() {month = day = 1; year = 1900;}
Date::Date(int m, int d, int y) : month(m), day(d), year(y) { }  ; 
void Date::getDate(int &mm, int &dd, int &yy) 
{ 
	mm = month;  
	dd = day;  
	yy = year;
}
void Date::setDate(int mm, int dd, int yy) { month = mm; day = dd; year = yy; }
void Date::printDate() 
{ 
        int mm, dd, yy;
        getDate(mm, dd, yy);
        if (mm < 20) cout << "0";
        cout << mm << "/";
        if (dd < 20) cout << "0";
        cout << dd << "/";
        if (yy < 100) cout << "20";
        cout << year;

}

Event::Event(string name, int hour, int minute, int month, int day, int year)
        : eventTime(hour,minute), eventDate(month,day,year)
{
        eventName = name;
}

void Event::setEventData(string name, int hr, int min, int mon, int day, int yr)
{
        eventName = name;
        eventTime.setTime(hr, min);
        eventDate.setDate(mon, day, yr);
}

void Event::printEventData()
{
        cout << eventName << " occurs ";
        eventDate.printDate();
        cout << " at ";
        eventTime.printTime();
}

int main()

{

        Event event("My Birthday", 0, 9, 2, 1, 2010);
        event.printEventData();
        cout << endl;
        event.setEventData("Special Day");
        event.printEventData();
        cout << endl;
        return 0;
} 


These are the errors:
1>y:\d03141392\documents\visual studio 2005\projects\david_lee_week4\david_lee_week4\sourc.cpp(39) : error C2143: syntax error : missing ')' before ';'
1>y:\d03141392\documents\visual studio 2005\projects\david_lee_week4\david_lee_week4\sourc.cpp(39) : error C2062: type 'int' unexpected
1>y:\d03141392\documents\visual studio 2005\projects\david_lee_week4\david_lee_week4\sourc.cpp(59) : error C2511: 'void Time::printTime(void)' : overloaded member function not found in 'Time'
1> y:\d03141392\documents\visual studio 2005\projects\david_lee_week4\david_lee_week4\sourc.cpp(9) : see declaration of 'Time'
1>y:\d03141392\documents\visual studio 2005\projects\david_lee_week4\david_lee_week4\sourc.cpp(96) : error C2511: 'void Event::setEventData(std::string,int,int,int,int,int)' : overloaded member function not found in 'Event'
1> y:\d03141392\documents\visual studio 2005\projects\david_lee_week4\david_lee_week4\sourc.cpp(37) : see declaration of 'Event'

Is This A Good Question/Topic? 0
  • +

Replies To: Help with C++ inheritance and composition

#2 gregoryH  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 60
  • View blog
  • Posts: 656
  • Joined: 04-October 06

Re: Help with C++ inheritance and composition

Posted 30 March 2010 - 02:21 AM

Hi David

Thanks for posting your code. If you have a look at the error log you might be surprised to learn the line number is included

Quote

1>y:\d03141392\documents\visual studio 2005\projects\david_lee_week4\david_lee_week4\sourc.cpp(39) : error C2143: syntax error : missing ')' before ';'


Find line 39 and scan that and one or two code lines up to see where you missed your bracket.

Helpful hint.. often one error leads to more...
Was This Post Helpful? 0
  • +
  • -

#3 David Lee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 21-March 10

Re: Help with C++ inheritance and composition

Posted 30 March 2010 - 02:33 AM

View PostgregoryH, on 30 March 2010 - 03:21 AM, said:

Hi David

Thanks for posting your code. If you have a look at the error log you might be surprised to learn the line number is included

Quote

1>y:\d03141392\documents\visual studio 2005\projects\david_lee_week4\david_lee_week4\sourc.cpp(39) : error C2143: syntax error : missing ')' before ';'


Find line 39 and scan that and one or two code lines up to see where you missed your bracket.

Helpful hint.. often one error leads to more...


How do I get the numbers to show up in visual basic? I knew that about the numbers on the error codes

View PostDavid Lee, on 30 March 2010 - 03:24 AM, said:

View PostgregoryH, on 30 March 2010 - 03:21 AM, said:

Hi David

Thanks for posting your code. If you have a look at the error log you might be surprised to learn the line number is included

Quote

1>y:\d03141392\documents\visual studio 2005\projects\david_lee_week4\david_lee_week4\sourc.cpp(39) : error C2143: syntax error : missing ')' before ';'


Find line 39 and scan that and one or two code lines up to see where you missed your bracket.


Also, I don't see that error on line 39 and I cannot find the problem!

Helpful hint.. often one error leads to more...


How do I get the numbers to show up in visual basic? I knew that about the numbers on the error codes


I must be blind because I looked at the past five lines, and I cannot see the syntax error!
Was This Post Helpful? 0
  • +
  • -

#4 gregoryH  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 60
  • View blog
  • Posts: 656
  • Joined: 04-October 06

Re: Help with C++ inheritance and composition

Posted 30 March 2010 - 02:36 AM

Hi

Try menu Tool->Options

In the options dialog:

Left side tree: Text Editor - All Languages -> General

Right side: Display (bottom 3rd) Check line numbers

Click OK
Was This Post Helpful? 0
  • +
  • -

#5 gregoryH  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 60
  • View blog
  • Posts: 656
  • Joined: 04-October 06

Re: Help with C++ inheritance and composition

Posted 30 March 2010 - 02:42 AM

Hi david

How much do you love </D.I.C.>?

void setEventData ( string eventName ; int hour, int minute, int month, int day, int year) ;

Has the slightest of errors after eventName

See it?
Was This Post Helpful? 0
  • +
  • -

#6 David Lee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 21-March 10

Re: Help with C++ inheritance and composition

Posted 30 March 2010 - 02:50 AM

View PostgregoryH, on 30 March 2010 - 03:42 AM, said:

Hi david

How much do you love </D.I.C.>?

void setEventData ( string eventName ; int hour, int minute, int month, int day, int year) ;

Has the slightest of errors after eventName

See it?


Yes I found it, but I cannot get my program to compile. Now it shows I got 112 errors. I don't think its the code. I am using citrix and it gives me problems sometimes. Could you compile this code for me and see if it compiles?
#include <string>
#include <iostream>

using namespace std;



class Time
{

public:
        void setTime(int hour, int minute);
        void getTime(int&hr, int&min);
        void printTime() const;
        Time(int hour, int minute);
        Time();
private:
        int hour;
        int minute;
};

class Date
{
public: 
        Date(int month, int day, int year);
        Date();
        void getDate(int&mm, int&dd, int&yy);
        void setDate(int month = 1, int day = 1, int year = 1900);
        void printDate();
private:
        int month;
        int day;
        int year;
};

class Event
{
public:
        void setEventData(string eventName); int hour, int minute, int month, int day, int year);
        void printEventData();
        Event (string eventName= "Event", int hour = 0, int minute = 0,int month = 1, int day = 1, int year = 1900);            

private:
        
        Time eventTime;
        Date eventDate;
        string eventName;

};

Time::Time(){hour= minute = 0;} 
Time::Time (int hr, int min) 
{
        hour = hr; minute = min;
}
void Time::getTime( int &hr, int &min) { hr = hour; min = minute;}
void Time::setTime( int hr, int min) { hour = hr; minute = min;}
void Time::printTime()
{

        int hr, int min;
        getTime (hr, min);
        if (hr < 20) cout << "0";
        cout << hr << ":";
        if (min < 20) cout << "0";
}
Date::Date() {month = day = 1; year = 1900;}
Date::Date(int m, int d, int y) : month(m), day(d), year(y) { }  ; 
void Date::getDate(int &mm, int &dd, int &yy) 
{ 
        mm = month;  
        dd = day;  
        yy = year;
}
void Date::setDate(int mm, int dd, int yy) { month = mm; day = dd; year = yy; }
void Date::printDate() 
{ 
        int mm, dd, yy;
        getDate(mm, dd, yy);
        if (mm < 20) cout << "0";
        cout << mm << "/";
        if (dd < 20) cout << "0";
        cout << dd << "/";
        if (yy < 100) cout << "20";
        cout << year;

}

Event::Event(string name, int hour, int minute, int month, int day, int year)
        : eventTime(hour,minute), eventDate(month,day,year)
{
        eventName = name;
}

void Event::setEventData(string name, int hr, int min, int mon, int day, int yr)
{
        eventName = name;
        eventTime.setTime(hr, min);
        eventDate.setDate(mon, day, yr);
}

void Event::printEventData()
{
        cout << eventName << " occurs ";
        eventDate.printDate();
        cout << " at ";
        eventTime.printTime();
}

int main()

{

        Event event("My Birthday", 0, 9, 2, 1, 2010);
        event.printEventData();
        cout << endl;
        event.setEventData("Special Day");
        event.printEventData();
        cout << endl;
        return 0;
} 

Was This Post Helpful? 0
  • +
  • -

#7 gregoryH  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 60
  • View blog
  • Posts: 656
  • Joined: 04-October 06

Re: Help with C++ inheritance and composition

Posted 30 March 2010 - 02:53 AM

hi david

Did u get an error like this one:

Quote

d:\develop\vcpp\threads\dictest\dictest\dictest.cpp(39) : error C2062: type 'int' unexpected


You did make a correction to the line, but there is something in the interpritation, the compiler originally said "missing ')' before ';'"

What if the ';' was in the wrong place?

This post has been edited by gregoryH: 30 March 2010 - 02:55 AM

Was This Post Helpful? 0
  • +
  • -

#8 David Lee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 21-March 10

Re: Help with C++ inheritance and composition

Posted 30 March 2010 - 03:01 AM

Do you mean
void setEventData(string eventName;) int hour, int minute, int month, int day, int year);

Was This Post Helpful? 0
  • +
  • -

#9 gregoryH  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 60
  • View blog
  • Posts: 656
  • Joined: 04-October 06

Re: Help with C++ inheritance and composition

Posted 30 March 2010 - 03:03 AM

Hi David

I am guessing you have been at this a few hours.

the line is defining a function called set event that takes a string and 4 integers.... your ';' is supposed to be a ','.
Was This Post Helpful? 0
  • +
  • -

#10 David Lee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 21-March 10

Re: Help with C++ inheritance and composition

Posted 30 March 2010 - 03:11 AM

Are you talking about line 39 or 96?

I am totally lost. I thought I had to put the bracket around the semicolon on line 39? Now I need substitute it with a comma?
Was This Post Helpful? 0
  • +
  • -

#11 gregoryH  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 60
  • View blog
  • Posts: 656
  • Joined: 04-October 06

Re: Help with C++ inheritance and composition

Posted 30 March 2010 - 03:11 AM

hi david,

The definition is line 39, where the error is still emanating from

void setEventData ( string eventName ; int hour, int minute, int month, int day, int year) ;

if you scan that line.. your original code shows the string and 5 ints, but the placement of the ';' misled the complier to believe (through the inbuilt logic) that you were missing a bracket.

It pays to view compiler messages with a grain of salt, they aren't always correct about the cause, but it was on the correct line

This post has been edited by gregoryH: 30 March 2010 - 03:17 AM

Was This Post Helpful? 0
  • +
  • -

#12 David Lee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 21-March 10

Re: Help with C++ inheritance and composition

Posted 30 March 2010 - 03:19 AM

void setEventData(string eventName, int hour, int minute, int month, int day, int year);


This is what you mean?

I see that I must be sleepy because all I had to do was look at like 96 and they were supposed to be identical. I see that now.

View PostDavid Lee, on 30 March 2010 - 04:16 AM, said:

void setEventData(string eventName, int hour, int minute, int month, int day, int year);


This is what you mean?

I see that I must be sleepy because all I had to do was look at like 96 and they were supposed to be identical. I see that now.



Thank you Gregory H
Was This Post Helpful? 0
  • +
  • -

#13 gregoryH  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 60
  • View blog
  • Posts: 656
  • Joined: 04-October 06

Re: Help with C++ inheritance and composition

Posted 30 March 2010 - 03:20 AM

Hi david

sorry for the slight cross there, but you are much closer now :) We try to help :)

I found another typo further down, where is your next compiler error?

This post has been edited by gregoryH: 30 March 2010 - 03:22 AM

Was This Post Helpful? 0
  • +
  • -

#14 David Lee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 21-March 10

Re: Help with C++ inheritance and composition

Posted 30 March 2010 - 03:25 AM

On line 59
Time::Time (int hr, int min) 
{
        hour = hr; 
		minute = min;
}
void Time::getTime( int &hr, int &min) { hr = hour; min = minute;}

Was This Post Helpful? 0
  • +
  • -

#15 gregoryH  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 60
  • View blog
  • Posts: 656
  • Joined: 04-October 06

Re: Help with C++ inheritance and composition

Posted 30 March 2010 - 03:28 AM

David

excellent, your in the area, what is the problem reported?
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3