compile error

P

pagantom

Hello. newbie here. need help. here's the text, compile errors are
below. thanks

class date
{
private:
char *mo;
int day, yr;

public:
date (char *m, int d, int y)
{mo = new char[strlen(m)+1];
strcpy(mo, m);
day = d;
yr = y;}

date(const date &obj)
{mo = new char[strlen(obj.mo)+1];
strcpy(mo, obj.mo);
day = obj.day;
yr = obj.yr;}

~date()
{delete[] mo;}

const char *getMo()
{return mo;}

int getDay()
{return day;}

int getYr()
{return yr;}

void operator=(const date &right)
{ delete [] mo;
mo = new char[strlen(right.mo) + 1];
strcpy(mo, right.mo);
day = right.day;
yr = right.yr;}
};

class time:public date
{
private:
int hr, min, sec;

public:

time (int hh, int mm, int ss)
{hr = hh;
min = mm;
sec = ss;}

time(const time &obj)
{hr = obj.hr;
min = obj.min;
sec = obj.sec;}

int getHour()
{return hr;}

int getMin()
{return min;}

int getSec()
{return sec;}
};

class date
{
private:
char *mo;
int day, yr;

public:
date (char *m, int d, int y)
{mo = new char[strlen(m)+1];
strcpy(mo, m);
day = d;
yr = y;}

date(const date &obj)
{mo = new char[strlen(obj.mo)+1];
strcpy(mo, obj.mo);
day = obj.day;
yr = obj.yr;}

~date()
{delete[] mo;}

const char *getMo()
{return mo;}

int getDay()
{return day;}

int getYr()
{return yr;}

void operator=(const date &right)
{ delete [] mo;
mo = new char[strlen(right.mo) + 1];
strcpy(mo, right.mo);
day = right.day;
yr = right.yr;}
};

class time:public date
{
private:
int hr, min, sec;

public:

time (int hh, int mm, int ss)
{hr = hh;
min = mm;
sec = ss;}

time(const time &obj)
{hr = obj.hr;
min = obj.min;
sec = obj.sec;}

int getHour()
{return hr;}

int getMin()
{return min;}

int getSec()
{return sec;}
};

Compile errors

In constructor `time::time(int, int, int)':
error: no matching function for call to `date::date()'
note: candidates are: date::date(const date&)
date::date(char*, int, int)

In copy constructor `time::time(const time&)':
error: no matching function for call to `date::date()'
candidates are: date::date(const date&)
date::date(char*, int, int)
 
K

kwikius

pagantom said:
Hello. newbie here. need help. here's the text, compile errors are
below. thanks

You need to add a constructor to your dat class that takes no
parameters, or use one of the constructors you have defined with its
parameters instead.

regards
Andy Little
 
K

kwikius

pagantom said:
Hello. newbie here. need help. here's the text, compile errors are
below. thanks

You need to add a constructor to your dat class that takes no
parameters, or use one of the constructors you have defined with its
parameters instead.

regards
Andy Little
 
Z

zouyongbin

"pagantom дµÀ£º
"
Hello. newbie here. need help. here's the text, compile errors are
below. thanks

The base class needs to be initialized with a default constructor here.
 
P

pagantom

"pagantom 写é“:
"

thanks, that did the trick. i added a main, but i'm getting more
compliation errors.
why are the "time" instances not being created?

int main()
{
time first("January", 1, 2007);

time second("January", 2, 2007);

time reverse = first;

cout << "first date = " << first.getMo();
cout << "/" << first.getDay();
cout << "/" << first.getYr() << endl;

cout << "second date = " << second.getMo();
cout << "/" << second.getDay();
cout << "/" << second.getYr() << endl;

reverse = second;
second = first;

cout << "now first date = " << reverse.getMo();
cout << "/" << reverse.getDay();
cout << "/" << reverse.getYr() << endl;

cout << "now second date = " << second.getMo();
cout << "/" << second.getDay();
cout << "/" << second.getYr() << endl;

int main()
{
time first("January", 1, 2007);

time second("January", 2, 2007);

time reverse = first;

cout << "first date = " << first.getMo();
cout << "/" << first.getDay();
cout << "/" << first.getYr() << endl;

cout << "second date = " << second.getMo();
cout << "/" << second.getDay();
cout << "/" << second.getYr() << endl;

reverse = second;
second = first;

cout << "now first date = " << reverse.getMo();
cout << "/" << reverse.getDay();
cout << "/" << reverse.getYr() << endl;

cout << "now second date = " << second.getMo();
cout << "/" << second.getDay();
cout << "/" << second.getYr() << endl;

In function `int main()':
error: expected `;' before "first"
statement is a reference, not call, to function `time'
error: expected `;' before "second"
warning: statement is a reference, not call, to function `time'
expected `;' before "reverse"
statement is a reference, not call, to function `time'
 
D

Default User

pagantom wrote:

int main()
{
time first("January", 1, 2007);

time second("January", 2, 2007);

time reverse = first;
In function `int main()':
error: expected `;' before "first"
statement is a reference, not call, to function `time'
error: expected `;' before "second"
warning: statement is a reference, not call, to function `time'
expected `;' before "reverse"
statement is a reference, not call, to function `time'

time() is a function, not a datatype. What is it you are trying to do?



Brian
 
M

Micah Cowan

Default said:
pagantom wrote:




time() is a function, not a datatype. What is it you are trying to do?

Well... no, std::time() is a function, but he hasn't #included <ctime>
or imported the standard namespace, so no immediate problem, though
it's still probably a pretty bad idea...

</pedantry> :)
 
D

Default User

Micah said:
Well... no, std::time() is a function, but he hasn't #included <ctime>
or imported the standard namespace, so no immediate problem, though
it's still probably a pretty bad idea...

</pedantry> :)

You were insufficiently pedantic. The C standard library and headers
that place the identifiers in the global namespace are still standard,
even though deprecated. So time() is a function, although the standard
states that it's as if there were an explicit using-declaration.

The point about headers is well taken, in fact the program shown had no
headers at all.



Brian
 
M

Micah Cowan

Default said:
You were insufficiently pedantic. The C standard library and headers
that place the identifiers in the global namespace are still standard,
even though deprecated. So time() is a function, although the standard
states that it's as if there were an explicit using-declaration.

That's only true if you #include <time.h> instead of <ctime>, no? (Not
that real-world implementations always work like that, which is part of
why it's a bad idea...)

-Micah
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top