why doesn't the compiler convert character array to my custom-made String class?

45 views Asked by At

I have a custom String class which is not the well-known built-in string class in C++. One of the constructors takes a const character array and looks like the following.

String::String(const char* const cString)
{
    itsLen = strlen(cString);
    itsString = new char[itsLen + 1];
    for (int i = 0; i < itsLen; i++) {
        itsString[i] = cString[i];
    };
    itsString[itsLen] = '\0';
}

Now I have an Employee class that takes 3 character arrays and 1 long, and its implementation looks like this.

Employee::Employee(char * fisrtName, char * lastName, char * address, long salary):
    itsFirstName(fisrtName),
    itsLastName(lastName),
    itsAddress(address),
    itsSalary(salary)
{}

Quite simple isn't it?

Now my question is, my compiler throws an error when I enter a string or a character array. However, my textbook does not mention of such error and its custom string class works normally.

int main() {
    String str = String("testing");
    Employee Yousef("Yousef", "Marey", "123 street", 20000); // error, aren't C-strings already char* ?
    Employee Edie(str, str, str, 500); // error, shouldn't the compiler convert type String into a C-String
}

Does anybody know the problem here?

Thanks.

0

There are 0 answers