| | |
Constructor overloading question
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Apr 2008
Posts: 3
Reputation:
Solved Threads: 0
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.
// Date.h
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
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> 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
•
•
Join Date: Apr 2008
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
date.cpp:
>>Date:Date
That should have two :, not one
Date::Date
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
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
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)
d:\dvlp\test2\test2\test2.cpp(30) : error C3861: 'print': identifier not found d:\dvlp\test2\test2\test2.cpp(36) : error C2143: syntax error : missing ';' before ':' d:\dvlp\test2\test2\test2.cpp(36) : error C2059: syntax error : ':' d:\dvlp\test2\test2\test2.cpp(36) : error C2062: type 'const char' unexpected d:\dvlp\test2\test2\test2.cpp(37) : error C2143: syntax error : missing ';' before '{' d:\dvlp\test2\test2\test2.cpp(37) : error C2447: '{' : missing function header (old-style formal list?) d:\dvlp\test2\test2\test2.cpp(41) : error C2143: syntax error : missing ';' before ':' d:\dvlp\test2\test2\test2.cpp(41) : error C2059: syntax error : ':' d:\dvlp\test2\test2\test2.cpp(41) : error C2062: type 'const char' unexpected d:\dvlp\test2\test2\test2.cpp(42) : error C2143: syntax error : missing ';' before '{' d:\dvlp\test2\test2\test2.cpp(42) : error C2447: '{' : missing function header (old-style formal list?) d:\dvlp\test2\test2\test2.cpp(48) : error C2470: 'Date' : looks like a function definition, but there is no parameter list; skipping apparent body d:\dvlp\test2\test2\test2.cpp(53) : error C2470: 'Date' : looks like a function definition, but there is no parameter list; skipping apparent body d:\dvlp\test2\test2\test2.cpp(61) : error C3861: 'print': identifier not found d:\dvlp\test2\test2\test2.cpp(97) : error C2664: 'Date::Date(int,int,int)' : cannot convert parameter 1 from 'const char [5]' to 'int' There is no context in which this conversion is possible d:\dvlp\test2\test2\test2.cpp(101) : error C2664: 'Date::Date(int,int,int)' : cannot convert parameter 1 from 'const char [9]' to 'int' There is no context in which this conversion is possible Build log was saved at "file://d:\dvlp\test2\test2\Debug\BuildLog.htm" test2 - 16 error(s), 0 warning(s) ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
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.
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Apr 2008
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
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.
![]() |
Similar Threads
- Operator precedence - overloading - (order of things) (C++)
- Declaration of variables used within the operators overloading function (C++)
- Constructor Question? (C#)
- Newbie Constructor / Array question (C++)
- Constrcutors and Destructors (C++)
- Overloading assignment operator (C++)
- overloading Operator << to accept endl (C)
Other Threads in the C++ Forum
- Previous Thread: plase help
- Next Thread: rand(); always ='s 41?
Views: 4192 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






