I have a compile time error in my C++ program that reads:
error C2440: 'default argument' : cannot convert from 'const char [1]' to 'const char *[]'
It's coming from here in my code in my .h file, this is the constructor:
Name(const char* firstNm[] = "", const char* lastNm[] = "");
In my .h file I have my private data members as:
char* firstName;
char* lastName;
In the actual .cpp document that implements my .h file I have:
Name::Name(const char* firstNm[], const char* lastNm[])
{
firstName = NULL;
lastName = NULL;
setFirstName(firstNm);
setLastName(lastNm);
}
void Name::setFirstName(const char* firstNm[])
{
if(firstName != NULL)
delete[].firstName;
if(strlen(firstNm) > 20){
firstName = new char[MAX_NAME+1];
assert(firstName != NULL);
strcpy(firstName, firstNm, MAX_NAME);
firstName[MAX_NAME]='\0'
cerr<<"First Name exceeds max name, will be truncated to 20th character."<<endl;
}
else{
firstName=new char[strlen(firstNm)+1]
assert(firstName != NULL);
strcpy(firstName,firstNm, MAX_NAME);
}
}
And similar code for the setLastName
and also, MAX_NAME is set to 20
We're starting to use pointer and reference variables and I'm still not too 'clear' as to how I need to go about doing this and pointing where.
Any help as to why I'm getting this error and what to understand would be great.
constchar* foo is a pointer to a character (or an array of characters) constchar foo[] is the same thing (when passed as a function parameter) constchar* foo[] is a pointer to an array of pointers (ie: a double pointer, not what you intended).
Change this to either const char* foo, or const char foo[].
Or better yet -- save yourself a million headaches and just use strings instead of char pointers.