943,729 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 8372
  • C++ RSS
Apr 17th, 2008
0

Constructor overloading question

Expand Post »
Hello,

I need to provide 2 overloaded constructors, each one displaying a date in a different format. Below is my class implementations, header file and main() function code snippets. The code is followed by the compilation error I get. I have tried a number of things, mainly changing the order of my constructors and such, but I keep getting this error. Could I get some expert guidance please? There is a lot of code here; some could have been abridged more, but I color-coded the code in question for easy readability (HOPEFULLY easy for you)

THANKS!!!

// Date.cpp
// Date class member-function definitions.
#include <iostream>
using std::cout;
using std::endl;
#include "Date.h" // include Date class definition

// constructor confirms proper value for month; calls
// utility function checkDay to confirm proper value for day

Date:Date( int mn, int dy, int yr )
{
if ( mn > 0 && mn <= 12 ) // validate the month
month = mn;
else 
{ 
month = 1; // invalid month set to 1
cout << "Invalid month (" << mn << ") set to 1.\n";
} // end else

year = yr; // could validate yr
day = checkDay( dy ); // validate the day

// output Date object to show when its constructor is called

cout << "Date object constructor for date ";
print(); 
cout << endl;

} // end Date constructor


Date:Date( const char mth, int yr ) // overloaded constructor for 2nd date format
{
monthW = mth;
year = yr;
}
Date:Date( const char mth , int dy, int yr ) // overloaded constructor for 3rd date format
{
monthW = mth;
day = dy;
year = yr;
}// print Date object in form month/day/year

void Date:print() const
{
cout << month << '/' << day << '/' << year; 
} // end function print
// print Date object in format of January 1, 2000
void Date:rint2() const
{
cout<< month << "" << day << "," << year;
} // end function print2
// output Date object to show when its destructor is called
Date::~Date()
{ 
cout << "Date object destructor for date ";
print();
cout << endl;
} // end ~Date destructor

// utility function to confirm proper day value based on 
// month and year; handles leap years, too
int Date::checkDay( int testDay ) const
{
static const int daysPerMonth[ 13 ] = 
{ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
// determine whether testDay is valid for specified month
if ( testDay > 0 && testDay <= daysPerMonth[ month ] )
return testDay;
// February 29 check for leap year 
if ( month == 2 && testDay == 29 && ( year % 400 == 0 || 
( year % 4 == 0 && year % 100 != 0 ) ) )
return testDay;
cout << "Invalid day (" << testDay << ") set to 1.\n";
return 1; // leave object in consistent state if bad value
} // end function checkDay

//---------------------------------------------------------------------

// Date.h
// Date class definition; Member functions defined in Date.cpp
#ifndef DATE_H
#define DATE_H
class Date 
{
public:
Date( int = 1, int = 1, int = 1900 ); // default constructor
Date( const char, int = 1900);//overloaded constructor 
Date( const char, int = 1, int = 1900); // 2nd overloaded constructorvoid print() const; // print date in month/day/year format
void print2() const; // print date in a January, 1, 2000 format
~Date(); // provided to confirm destruction order

private:
int month; // 1-12 (January-December)
int day; // 1-31 based on month
int year; // any year
char monthW; // month spelled out
// utility function to check if day is proper for month and year
int checkDay( int ) const; 
}; // end class Date
#endif
//---------------------------------------------------------------------
// Fig. 10.14: fig10_14.cpp
// Demonstrating composition--an object with member objects.
#include <iostream>
using std::cout;
using std::endl;
#include <ctime>
using std::time;
#include "Employee.h" // Employee class definition
int main()
{
time_t date; // time_t object to hold the date
time(&date); //set date variable to current date
Date birth( 7, 24, 1949 ); // using constructor
Date hire( "July", 1988 ); // overloaded constructor (first one with different date format)
Employee manager( "Bob", "Blue", birth, hire );cout << endl;
manager.print();
cout<<"\nTest Date constructor with valid valuesn";
Date lastDayOff("December", 25, 2007); // overloaded constructor (second one with different date format)cout<<"Employee's last day off: \n";
lastDayOff.print2(); // print overloaded arguments
cout<<endl;
cout<<"Date of report: "<<ctime(&date)<<endl;
return 0;
} // end main
//-----------------------------------------------------------------------
1>cprogram files\c++examples\cpphtp6e_examples\ch10\fig10_10_ 14\fig10_14.cpp(19) : error C2664: 'Date:ate(int,int,int)' : cannot convert parameter 1 from 'const char [5]' to 'int'
1> There is no context in which this conversion is possible
1>cprogram files\c++examples\cpphtp6e_examples\ch10\fig10_10_ 14\fig10_14.cpp(26) : error C2664: 'Date:ate(int,int,int)' : cannot convert parameter 1 from 'const char [9]' to 'int'
1> There is no context in which this conversion is possible
Last edited by Ancient Dragon; Apr 17th, 2008 at 1:33 pm. Reason: add code tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bschmitt78 is offline Offline
6 posts
since Apr 2008
Apr 17th, 2008
0

Re: Constructor overloading question

date.cpp:
>>Date:Date
That should have two :, not one
Date::Date
Last edited by Ancient Dragon; Apr 17th, 2008 at 1:38 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Apr 17th, 2008
0

Re: Constructor overloading question

date.cpp:
>>Date:Date
That should have two :, not one
Date::Date
There were 2 ::'s in there when I got this compile error. I think when I copied the code into this forum, some characters got deleted by accident. I just checked my code now in my program and there are 2 ::'s.

Sorry for not following the rules carefully when I posted this. I noticed you had to clean it up quite a bit - thanks!
Last edited by Ancient Dragon; Apr 17th, 2008 at 2:08 pm. Reason: disabled smilies
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bschmitt78 is offline Offline
6 posts
since Apr 2008
Apr 17th, 2008
0

Re: Constructor overloading question

using VC++ 2008 Express, I don't get that error after correcting the 2 :: s
The code you posted contains a lot of other errors too. There are several lines with only one :, and Date::print() is named Date::print2() in the header file.

Here's the errors I get now
C++ Syntax (Toggle Plain Text)
  1. d:\dvlp\test2\test2\test2.cpp(30) : error C3861: 'print': identifier not found
  2. d:\dvlp\test2\test2\test2.cpp(36) : error C2143: syntax error : missing ';' before ':'
  3. d:\dvlp\test2\test2\test2.cpp(36) : error C2059: syntax error : ':'
  4. d:\dvlp\test2\test2\test2.cpp(36) : error C2062: type 'const char' unexpected
  5. d:\dvlp\test2\test2\test2.cpp(37) : error C2143: syntax error : missing ';' before '{'
  6. d:\dvlp\test2\test2\test2.cpp(37) : error C2447: '{' : missing function header (old-style formal list?)
  7. d:\dvlp\test2\test2\test2.cpp(41) : error C2143: syntax error : missing ';' before ':'
  8. d:\dvlp\test2\test2\test2.cpp(41) : error C2059: syntax error : ':'
  9. d:\dvlp\test2\test2\test2.cpp(41) : error C2062: type 'const char' unexpected
  10. d:\dvlp\test2\test2\test2.cpp(42) : error C2143: syntax error : missing ';' before '{'
  11. d:\dvlp\test2\test2\test2.cpp(42) : error C2447: '{' : missing function header (old-style formal list?)
  12. d:\dvlp\test2\test2\test2.cpp(48) : error C2470: 'Date' : looks like a function definition, but there is no parameter list; skipping apparent body
  13. d:\dvlp\test2\test2\test2.cpp(53) : error C2470: 'Date' : looks like a function definition, but there is no parameter list; skipping apparent body
  14. d:\dvlp\test2\test2\test2.cpp(61) : error C3861: 'print': identifier not found
  15. d:\dvlp\test2\test2\test2.cpp(97) : error C2664: 'Date::Date(int,int,int)' : cannot convert parameter 1 from 'const char [5]' to 'int'
  16. There is no context in which this conversion is possible
  17. d:\dvlp\test2\test2\test2.cpp(101) : error C2664: 'Date::Date(int,int,int)' : cannot convert parameter 1 from 'const char [9]' to 'int'
  18. There is no context in which this conversion is possible
  19. Build log was saved at "file://d:\dvlp\test2\test2\Debug\BuildLog.htm"
  20. test2 - 16 error(s), 0 warning(s)
  21. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Apr 17th, 2008
0

Re: Constructor overloading question

After correcting a bunch of errors I'm back to your original question.

The problem is that there is no constructor with the first paramter as const char*, only const char, which of course is not the same thing.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Apr 17th, 2008
0

Re: Constructor overloading question

After correcting a bunch of errors I'm back to your original question.


OK. I think the "other errors" you referred to came up in the way I posted my code. I only had the 2 errors I referred to before. Also, I left out the "Employee.h file because it wasn't really relevant to my problem.

The problem is that there is no constructor with the first paramter as const char*, only const char, which of course is not the same thing.
OK, thanks, that makes sense now that I look at it. But isn't a pointer to a const char supposed to be "const char * const someVariable"? Should I just be using const char * someVariable"?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bschmitt78 is offline Offline
6 posts
since Apr 2008
Apr 17th, 2008
0

Re: Constructor overloading question

Depends on how you want to use it. See this
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: plase help
Next Thread in C++ Forum Timeline: rand(); always ='s 41?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC