#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
using namespace std;
class MyFriendInfo
{
private:
char* name;
int age;
public:
MyFriendInfo(const char* fname, int fage) : age(fage) {
name = new char[strlen(fname) + 1];
strcpy(name, fname);
}
void ShowMyFriendInfo() {
cout << "Name: " << name << endl;
cout << "Age:" <<age << endl;
}
~MyFriendInfo() {
delete []name;
}
};
class MyFriendDetailInfo : public MyFriendInfo
{
private:
char* addr;
char* phone;
public:
MyFriendDetailInfo(const char* fname, int fage, char* faddr, char* fphone) :MyFriendInfo(fname, fage) {
addr = new char[strlen(faddr) + 1];
phone = new char[strlen(fphone) + 1];
strcpy(addr, faddr);
strcpy(phone, fphone);
}
void ShowMyFriendDetailInfo() {
ShowMyFriendInfo();
cout << "Address: " << addr << endl;
cout << "Phone: " << phone << endl;
}
~MyFriendDetailInfo() {
delete []addr;
delete []phone;
}
};
int main(void) {
MyFriendDetailInfomyFriend ("Name", 23, "Address", "01075455778");
myFriend.ShowMyFriendDetailInfo();
return 0;
}
I'm studying with Yoon Sung-woo's C++ textbook, and an error message appears as the third line ("name") is red at the bottom. How can I solve it?
constructor
The reason for the error is not because of "Name", but because of "Address" and "01075455778".
MyFriendDetailInfo(const char* fname, int fage, char* faddr, char* fphone) :MyFriendInfo(fname, fage)
Among the codes you uploaded, if you look at the constructor function of the MyFriendDetailInfo class above, fname is const char*
and faddr and fphone are char*
.
Faddr and fphone should also be written as const char*
as shown below. I think it might have been a typo while typing the book code.
MyFriendDetailInfo(const char* fname, int fage, const char* faddr, const char* fphone) :MyFriendInfo(fname, fage)
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
using namespace std;
class MyFriendInfo
{
private:
char* name;
int age;
public:
MyFriendInfo(const char* fname, int fage) : age(fage) {
name = new char[strlen(fname) + 1];
strcpy(name, fname);
}
void ShowMyFriendInfo() {
cout << "Name: " << name << endl;
cout << "Age:" <<age << endl;
}
~MyFriendInfo() {
delete[]name;
}
};
class MyFriendDetailInfo : public MyFriendInfo
{
private:
char* addr;
char* phone;
public:
MyFriendDetailInfo(const char* fname, int fage, const char* faddr, const char* fphone) :MyFriendInfo(fname, fage) {
addr = new char[strlen(faddr) + 1];
phone = new char[strlen(fphone) + 1];
strcpy(addr, faddr);
strcpy(phone, fphone);
}
void ShowMyFriendDetailInfo() {
ShowMyFriendInfo();
cout << "Address: " << addr << endl;
cout << "Phone: " << phone << endl;
}
~MyFriendDetailInfo() {
delete[]addr;
delete[]phone;
}
};
int main(void) {
MyFriendDetailInfomyFriend ("Name", 23, "Address", "01075455778");
myFriend.ShowMyFriendDetailInfo();
return 0;
}
It may depend on the version of the compiler you use, but now implicit type transformation issues a warning or error.
Please explicitly convert to char* as below.
MyFriendDetailInfomyFriend((char*)"Name",23,(char*)"Address",(char*)"01075455778"));
© 2024 OneMinuteCode. All rights reserved.