Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Returning a character string

Status
Not open for further replies.

DerPflug

Programmer
Joined
Mar 28, 2002
Messages
153
Location
US
Given the following member function, how can I return a character string (an array element)? I keep getting an error that says that it "cannot convert from 'char' to 'char Date::*' ". I also have an error that says that 'M' is an undeclared identifier. However, I have this member function declared as a private member of my class as well as the variable 'M'. Any help would be appreciated.


class Date
{
int M,D,Y,DOY;
bool leapyear;
char *ly;
bool IsLeapYear();
void CalcDOY();
void ConvertToMthDay();
char *Get_Month_Name(char Option);

public:
void show();
int Get_Day();
int Get_Month();
int Get_Year();
int Get_DOY();
bool Get_Leap_Year();
Date(int M2,int D2,int Y2); //constructor
Date(int doy, int yr); //constructor
Date(); //constructor
};



char Date::*Get_Month_Name(char Option)
{
char full_name[12][10] =
{"January", "February", "March", "April", "May", "June", "July", "August", "September",
"October", "November", "December"};
char abbr_name[12][9] =
{"JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"};

switch (Option) {
case 'F':
return full_name[12][M - 1];
case 'A':
return abbr_name[12][M - 1];
}
}
 
char Date::*Get_Month_Name(char Option)
should be
char* Date::Get_Month_Name(char Option)

/Per
Nerdy signatures are as lame as the inconsistent stardates of STTNG.
 
as far as I see you have to problems:
1st is easy to solve, try
char *Date::Get_Month_Name(char Option) instead of
char Date::*Get_Month_Name(char Option)
(note: name of function is Date::Get_Month_Name, type is char *), this should include the M-Problem
2nd problem is:

as far as I know if you returns a char[] you only return a pointe, so that after end of function the content may be corrupt.
So you should use CString instead of this (is copied by copy-constructor) or make full_name and abbr_name as global (static const)...

Hope Could help you
[rednose]




Greetings Andreas
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top