回答をもとに、プログラミングのコードを修正しました。
回答頂いた方、ありがとうございます。
エラーに関して、stringで定義しており、char型に変換していないのに、エラーが起きており解決方法が分かりません。
すいませんが、教えて頂けたら幸いです。
###前提・実現したいこと
Worker.cpp で Phoneクラスのオブジェクトをコンポジションとして持っています。
その後、WorkerクラスをSalesクラスで継承しています。
Workerクラスでは、Phoneコンストラクタでメンバイニシャライザをしており、Salesクラスでイニシャライザを行っています。
その際、Salesのコンストラクタでイニシャライザをしているのにも関わらず、phoneクラスの引数が分からないとエラーで表示されるのですが、
どのように宣言したらよろしいのでしょうか。
ご回答頂けたら幸いです。
###発生している問題・エラーメッセージSales.cpp:11:3: error: no matching function for call to 'strcpy'
strcpy(this->addtionalInfo, addtionalInfo);
^~~~~~
/usr/include/string.h:129:14: note: candidate function not viable: no known conversion from 'std::string' (aka 'basic_string<char, char_traits<char>, allocator<char> >') to 'char *restrict' for 1st argument
extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
^
Sales.cpp:18:5: error: no matching function for call to 'strcpy'
strcpy(this->addtionalInfo, obj.addtionalInfo);
^~~~~~
/usr/include/string.h:129:14: note: candidate function not viable: no known conversion from 'std::string' (aka 'basic_string<char, char_traits<char>, allocator<char> >') to 'char *restrict' for 1st argument
extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
^
Sales.cpp:28:3: error: no matching function for call to 'strcpy'
strcpy(this->addtionalInfo, "Undifined_addtionalInfo");
^~~~~~
/usr/include/string.h:129:14: note: candidate function not viable: no known conversion from 'std::string' (aka 'basic_string<char, char_traits<char>, allocator<char> >') to 'char *restrict' for 1st argument
extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
^
Sales.cpp:33:33: error: no member named 'pInfo' in 'Sales'
cout << "s1.pInfo = " << s1.pInfo << "\n";
~~ ^
4 errors generated.
###ソースコード
<Sales.cpp> #include <iostream> #include <string.h> #include "Sales.h" using namespace std; Sales::Sales(double profit, char* client, std::string addtionalInfo, int number, char* name, double salary, bool i, char* plan, std::string pInfo):Worker(number, name, salary, i, plan, pInfo){ this->profit = profit; this->client = new char[100]; strcpy(this->client, client); strcpy(this->addtionalInfo, addtionalInfo); } Sales::Sales(const Sales &obj){ client = new char[100]; strcpy(client,obj.name); this->profit =obj.profit; strcpy(this->addtionalInfo, obj.addtionalInfo); } Sales::~Sales(){ } Sales::Sales():Worker(){ this->profit = 100; this->client = new char[100]; strcpy(this->client, "Undifined_client"); strcpy(this->addtionalInfo, "Undifined_addtionalInfo"); } void Sales::ShowData(Sales s1){ cout << "s1.name = " << s1.name << "\n"; cout << "s1.pInfo = " << s1.pInfo << "\n"; cout << "s1.profit = " << s1.profit << "\n"; cout << "s1.client = " << s1.client << "\n"; cout << " s1.addtionalInfo = " << s1.addtionalInfo << "\n"; cout << " s1.addtionalInfo " << s1.addtionalInfo << "\n"; } int main(){ Sales s1; return 0; } <Sales.h> #include <string.h> #include "Worker.h" class Sales:public Worker{ public: double profit; char* client; std::string addtionalInfo; Sales(); Sales(double profit, char* client, std::string addtionalInfo, int number, char* name, double salary, bool i, char* plan, std::string pInfo); Sales(const Sales &obj); ~Sales(); void ShowData(Sales s1); }; <Worker.cpp> #include <iostream> #include "Worker.h" using namespace std; Worker::Worker(){ name = new char[80]; strcpy(name, "undifined in Worker con"); number = 0; salary = 0; } Worker::Worker(int number, char* name, double salary,bool i, char* plan, string pInfo):phone(i ,plan ,pInfo){ cout<< " This is Constructor called Worker(int number, char* name, double salary):Phone(bool i, char* plan, string pInfo) " << "\n"; this->number = number; this->name = new char[80]; strcpy(this->name, name); this->salary = salary; } Worker::Worker(const Worker &obj){ name = new char[80]; strcpy(name,obj.name); this->number =obj.number; this->salary = obj.salary; }; Worker::~Worker(){ delete[] name; } /* int main(){ Worker w1; strcpy(w1.name,"Takayuki"); //Worker::Worker(int number, char* name, double salary,bool i, char* plan, string pInfo): w1.number =10; w1.salary = 200; ShowData(w1); //Worker::Worker(int number, char* name, double salary,bool i, char* plan, string pInfo): Worker w2(1000, "name_w2", 1000, false, "plan_w2", "pInfo_w2"); ShowData(w2); return 0; } */ <Worker.h> #include <string.h> #include "Phone.h" class Worker{ public: int number; char* name; double salary; Phone phone; Worker(); Worker(int number, char* name, double salary, bool i, char* plan, std::string pInfo); Worker(const Worker &obj); ~Worker(); }; <Phone.h> #include <string.h> class Phone{ public: Phone(); Phone(bool i, char* plan, std::string pInfo); ~Phone(); bool i; char* plan; std::string pInfo; void ShowPhone(); virtual void ShowData(); }; <Phone.cpp> #include <iostream> #include <string.h> #include "Phone.h" using namespace std; Phone::Phone(){ plan = new char[100]; this->i = false; strcpy(this->plan, "normal"); this->pInfo = "nokia"; } Phone::Phone(bool i, char* plan, string pInfo){ this->plan = new char[100]; this->i = i; strcpy(this->plan, plan); this->pInfo = "nokia"; } Phone::~Phone(){ delete[] plan; } void Phone::ShowPhone(){ cout << " i = " << this->i << "\n"; cout << " plan = " << this->plan <<"\n"; cout << " pInfo = " << this->pInfo <<"\n"; } void Phone::ShowData(){ } /* int main(){ Phone p1; Phone p2(false, "p2","noraml2"); Phone p3(false, "p3","noraml3"); cout << "----------p2.ShowPhone();-------------------" << "\n"; p2.ShowPhone(); cout << "----------p3.ShowPhone();-------------------" << "\n"; p3.ShowPhone(); cout << "-----------------------------" << "\n"; p1.i = false; strcpy(p1.plan,"normal"); p1.pInfo = "nokia"; p1.ShowPhone(); cout << "-----------------------------" << "\n"; p2.ShowData(); return 0; } */
###補足情報(言語/FW/ツール等のバージョンなど)
paiza.io
回答3件
あなたの回答
tips
プレビュー