質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Q&A

解決済

3回答

3009閲覧

C++ : メンバーイニシャライザと継承

saito.kaz

総合スコア76

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

0グッド

0クリップ

投稿2016/01/22 07:43

編集2016/01/22 08:51

回答をもとに、プログラミングのコードを修正しました。
回答頂いた方、ありがとうございます。

エラーに関して、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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

izkn

2016/01/22 08:52

こちらの質問が他のユーザから「やってほしいことだけを記載した丸投げの質問」という指摘を受けました 「質問を編集する」ボタンから編集を行い、調査したこと・試したことを記入していただくと、回答が得られやすくなります。
guest

回答3

0

C++

1strcpy(this->addtionalInfo, addtionalInfo);

上記のaddtionalInfoの型はstd::stringになっています。
stringクラスのメソッドを確認すればわかると思いますが、基本的には、strcpyは使用できません。

文字列のコピーは代入でできます。
以下の様にすれば良いと思います。

C++

1this->addtionalInfo = addtionalInfo; 2//また、文字列を代入するときは、以下でOKです。 3this->addtionalInfo = "1234";

std::stringのクラスを確認してみてください。

投稿2016/01/22 15:09

akiruno-oneone

総合スコア815

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

saito.kaz

2016/01/24 13:21

エラー文の意味を理解しても、なかなかどうすれば考えられる知識がないので、 困る点が多くあるのですが、勉強します。 ご回答ありがとうございます。
guest

0

エラーメッセージはよく確認しましょう。

./Sales.h:12:5: error: C++ requires a type specifier for all declarations →ShowData()の返り値型がありません。

Sales::Sales(double profit, char* client, std::string addtionalInfo ):Worker(number, name, salary, i, plan, pInfo)→Workerのイニシャライザに渡す値が、どこでも宣言されていません。

投稿2016/01/22 07:52

maisumakun

総合スコア145121

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

saito.kaz

2016/01/22 08:11

イニシャライザを利用することで、Workerクラスにあるコンストラクタの初期値を利用できると思っていたのですが、構文の定義がおかしいのでしょうか。 すいませんが、お願い致します。
saito.kaz

2016/01/24 13:21

エラー文の意味を理解しても、なかなかどうすれば考えられる知識がないので、 困る点が多くあるので、勉強不足だととても痛感します。 ご回答ありがとうございます。
guest

0

ベストアンサー

こんにちは。

回答依頼を頂きましたので、やってきました。
が、これ、前回と同じなのですよ。

前回> Worker::Worker(int number, char* name, double salary):Phone(i,plan,pInfo){

i, plan, pInfoがWorker::Worker()のパラメータにないことが原因でしたね。

今回> Sales::Sales(double profit, char* client, std::string addtionalInfo ):
今回> Worker(number, name, salary, i, plan, pInfo){

やはり、i, plan, pInfoがSales::Sales()のパラメータにないので、グローバル変数を探しに行っています。対策も前回と同じでできます。number, nameで同じエラーがでていないのがちょっと不思議ですが。


【追記】
新しい質問へ回答します。

strcpy(this->addtionalInfo, obj.addtionalInfo);

obj.addtionalInfoはstd::string型です。
strcpy()の第2パラメータはchar const型を受取ります。
std::string型からchar const
型への暗黙の型変換はありません。
従って、型違いでエラーになっています。

std::string型は、c_str()メンバ関数でchar const*型を返却しますので下記のように記述すれば良いです。

strcpy(this->addtionalInfo, obj.addtionalInfo.c_str());


【追記】
あぁぁ、コピー先もstd:stringであることを見落としてました。
この私の回答は間違いです。akiruno-oneoneさんの回答が正解です。

投稿2016/01/22 08:38

編集2016/01/22 15:52
Chironian

総合スコア23272

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

saito.kaz

2016/01/22 08:40

ありがとうございます。 すいません、メンバイニシャライザとイニシャライザで定義が同じでした。 お時間を無駄にしてしまい、本当に申し訳ないです。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問