質問するログイン新規登録

質問編集履歴

5

表示ミスの修正

2016/01/22 03:33

投稿

saito.kaz
saito.kaz

スコア76

title CHANGED
File without changes
body CHANGED
@@ -9,7 +9,8 @@
9
9
  なぜか下記コードを実装したところ、[phoneクラスがすでに実装されている]と言われ、イニシャライザがうまく実装できません。
10
10
  構文もあっていると思っているので、エラーの解決方法が分からないので、教えて頂けると幸いです。
11
11
 
12
+ ###発生している問題・エラーメッセージ
12
- ###発生している問題・エラーメッセージPhone.cpp:40:21: warning: ISO C++11 does not allow conversion from string literal to 'char *' [-Wwritable-strings]
13
+ Phone.cpp:40:21: warning: ISO C++11 does not allow conversion from string literal to 'char *' [-Wwritable-strings]
13
14
  Phone p2(false, "p2","noraml2");
14
15
  ^
15
16
  Phone.cpp:41:21: warning: ISO C++11 does not allow conversion from string literal to 'char *' [-Wwritable-strings]

4

*修正 : 回答を元に、エラー文、Workerクラスのプログラムを修正を行いました。

2016/01/22 03:33

投稿

saito.kaz
saito.kaz

スコア76

title CHANGED
File without changes
body CHANGED
@@ -2,36 +2,40 @@
2
2
  C++の学習を初めて1週間なので、質問が多くて申し訳ありません。
3
3
  質問なのですが、コンポジションの場合、仮想関数をオーバーライドすることはできないのでしょうか。
4
4
 
5
+ *修正 : 回答を元に、エラー文、Workerクラスのプログラムを修正を行いました。
6
+
5
7
  ###前提・実現したいこと
6
8
  下記のコードでは、phoneクラスを使って、Workerクラスでコンポジションを実装しています。
7
9
  なぜか下記コードを実装したところ、[phoneクラスがすでに実装されている]と言われ、イニシャライザがうまく実装できません。
8
10
  構文もあっていると思っているので、エラーの解決方法が分からないので、教えて頂けると幸いです。
9
11
 
10
- ###発生している問題・エラーメッセージ
11
- Phone.cpp:40:21: warning: ISO C++11 does not allow conversion from string literal to 'char *' [-Wwritable-strings]
12
+ ###発生している問題・エラーメッセージPhone.cpp:40:21: warning: ISO C++11 does not allow conversion from string literal to 'char *' [-Wwritable-strings]
12
13
  Phone p2(false, "p2","noraml2");
13
14
  ^
14
15
  Phone.cpp:41:21: warning: ISO C++11 does not allow conversion from string literal to 'char *' [-Wwritable-strings]
15
16
  Phone p3(false, "p3","noraml3");
16
17
  ^
17
18
  2 warnings generated.
18
- Phone.cpp:40:21: warning: ISO C++11 does not allow conversion from string literal to 'char *' [-Wwritable-strings]
19
- Phone p2(false, "p2","noraml2");
19
+ In file included from Worker.cpp:2:
20
+ ./Worker.h:8:4: error: unknown type name 'Phone'
21
+ Phone phone;
20
- ^
22
+ ^
21
- Phone.cpp:41:21: warning: ISO C++11 does not allow conversion from string literal to 'char *' [-Wwritable-strings]
22
- Phone p3(false, "p3","noraml3");
23
- ^
24
- 2 warnings generated.
25
- Worker.cpp:14:61: error: use of undeclared identifier 'i'
23
+ Worker.cpp:16:61: error: use of undeclared identifier 'i'
26
24
  Worker::Worker(int number, char* name, double salary):Phone(i,plan,pInfo){
27
25
  ^
28
- Worker.cpp:14:63: error: use of undeclared identifier 'plan'
26
+ Worker.cpp:16:63: error: use of undeclared identifier 'plan'
29
27
  Worker::Worker(int number, char* name, double salary):Phone(i,plan,pInfo){
30
28
  ^
31
- Worker.cpp:14:68: error: use of undeclared identifier 'pInfo'
29
+ Worker.cpp:16:68: error: use of undeclared identifier 'pInfo'
32
30
  Worker::Worker(int number, char* name, double salary):Phone(i,plan,pInfo){
33
31
  ^
32
+ Worker.cpp:32:9: error: exception specification in declaration does not match previous declaration
33
+ Worker::~Worker(){
34
+ ^
35
+ ./Worker.h:12:4: note: previous declaration is here
36
+ ~Worker();
37
+ ^
34
- 3 errors generated.
38
+ 5 errors generated.
35
39
  ###ソースコード
36
40
  ```
37
41
  <Phone.h>
@@ -126,12 +130,11 @@
126
130
 
127
131
 
128
132
 
129
-
130
- <Worker.cpp>
131
133
  #include <iostream>
132
134
  #include "Worker.h"
133
135
  #include "Phone.h"
134
136
 
137
+
135
138
  using namespace std;
136
139
 
137
140
  Worker::Worker(){
@@ -146,7 +149,7 @@
146
149
  cout<< " This is Constructor called Worker(int number, char* name, double salary):Phone(bool i, char* plan, string pInfo) " << "\n";
147
150
  this->number = number;
148
151
  this->name = new char[100];
149
- strcpy(this->name, name);
152
+ strcpy(this->name, name);
150
153
  this->salary = salary;
151
154
  }
152
155
 
@@ -184,6 +187,7 @@
184
187
  return 0;
185
188
  }
186
189
 
190
+
187
191
  ```
188
192
 
189
193
  ###補足情報(言語/FW/ツール等のバージョンなど)

3

Worker コンストラクタにWorker::を追加し、それに伴うエラー変更を追加

2016/01/22 03:30

投稿

saito.kaz
saito.kaz

スコア76

title CHANGED
File without changes
body CHANGED
@@ -15,17 +15,23 @@
15
15
  Phone p3(false, "p3","noraml3");
16
16
  ^
17
17
  2 warnings generated.
18
- In file included from Worker.cpp:3:
18
+ Phone.cpp:40:21: warning: ISO C++11 does not allow conversion from string literal to 'char *' [-Wwritable-strings]
19
- ./Phone.h:3:7: error: redefinition of 'Phone'
19
+ Phone p2(false, "p2","noraml2");
20
- class Phone{
21
- ^
20
+ ^
21
+ Phone.cpp:41:21: warning: ISO C++11 does not allow conversion from string literal to 'char *' [-Wwritable-strings]
22
- ./Phone.h:3:7: note: previous definition is here
22
+ Phone p3(false, "p3","noraml3");
23
- class Phone{
24
- ^
23
+ ^
24
+ 2 warnings generated.
25
+ Worker.cpp:14:61: error: use of undeclared identifier 'i'
26
+ Worker::Worker(int number, char* name, double salary):Phone(i,plan,pInfo){
27
+ ^
28
+ Worker.cpp:14:63: error: use of undeclared identifier 'plan'
29
+ Worker::Worker(int number, char* name, double salary):Phone(i,plan,pInfo){
30
+ ^
25
- Worker.cpp:7:18: error: type 'Phone' is not a direct or virtual base of 'Worker'
31
+ Worker.cpp:14:68: error: use of undeclared identifier 'pInfo'
26
- Worker::Worker():Phone(){
32
+ Worker::Worker(int number, char* name, double salary):Phone(i,plan,pInfo){
27
- ^~~~~
33
+ ^
28
- 2 errors generated.
34
+ 3 errors generated.
29
35
  ###ソースコード
30
36
  ```
31
37
  <Phone.h>
@@ -136,7 +142,7 @@
136
142
  salary = 0;
137
143
  }
138
144
 
139
- Worker(int number, char* name, double salary):Phone(i,plan,pInfo){
145
+ Worker::Worker(int number, char* name, double salary):Phone(i,plan,pInfo){
140
146
  cout<< " This is Constructor called Worker(int number, char* name, double salary):Phone(bool i, char* plan, string pInfo) " << "\n";
141
147
  this->number = number;
142
148
  this->name = new char[100];

2

Workerコンストラクタにthis->name = new char\[100\]; strcpy\(this->name, name\);を追加。

2016/01/22 02:22

投稿

saito.kaz
saito.kaz

スコア76

title CHANGED
File without changes
body CHANGED
@@ -140,6 +140,7 @@
140
140
  cout<< " This is Constructor called Worker(int number, char* name, double salary):Phone(bool i, char* plan, string pInfo) " << "\n";
141
141
  this->number = number;
142
142
  this->name = new char[100];
143
+ strcpy(this->name, name);
143
144
  this->salary = salary;
144
145
  }
145
146
 

1

Worker\.cppの仮想関数の設定を変更しました。

2016/01/22 02:15

投稿

saito.kaz
saito.kaz

スコア76

title CHANGED
@@ -1,1 +1,1 @@
1
- C++ 継承とイシャライザについて
1
+ C++ コンポジション、仮想関数とイシャライザについて
body CHANGED
@@ -1,5 +1,6 @@
1
1
  いつもお世話になっております。
2
2
  C++の学習を初めて1週間なので、質問が多くて申し訳ありません。
3
+ 質問なのですが、コンポジションの場合、仮想関数をオーバーライドすることはできないのでしょうか。
3
4
 
4
5
  ###前提・実現したいこと
5
6
  下記のコードでは、phoneクラスを使って、Workerクラスでコンポジションを実装しています。
@@ -41,6 +42,9 @@
41
42
  void ShowPhone();
42
43
  virtual void ShowData();
43
44
  };
45
+
46
+
47
+
44
48
  <Phone.cpp>
45
49
  #include <iostream>
46
50
  #include <string.h>
@@ -99,10 +103,9 @@
99
103
  return 0;
100
104
  }
101
105
 
106
+
107
+
102
108
  <Worker.h>
103
- #include <string.h>
104
- #include "Phone.h"
105
-
106
109
  class Worker{
107
110
  public:
108
111
  int number;
@@ -112,9 +115,12 @@
112
115
  Worker();
113
116
  Worker(int number, char* name, double salary);
114
117
  Worker(const Worker &obj);
115
- virtual void ShowDate();
116
118
  ~Worker();
117
119
  };
120
+
121
+
122
+
123
+
118
124
  <Worker.cpp>
119
125
  #include <iostream>
120
126
  #include "Worker.h"
@@ -122,7 +128,7 @@
122
128
 
123
129
  using namespace std;
124
130
 
125
- Worker::Worker():Phone(){
131
+ Worker::Worker(){
126
132
  name = new char[80];
127
133
  cout<< " This is Constructor " << "\n";
128
134
  strcpy(name, "undifined");
@@ -130,15 +136,47 @@
130
136
  salary = 0;
131
137
  }
132
138
 
139
+ Worker(int number, char* name, double salary):Phone(i,plan,pInfo){
140
+ cout<< " This is Constructor called Worker(int number, char* name, double salary):Phone(bool i, char* plan, string pInfo) " << "\n";
141
+ this->number = number;
142
+ this->name = new char[100];
143
+ this->salary = salary;
144
+ }
145
+
133
146
  Worker::Worker(const Worker &obj){
134
147
  cout<< " This is Copy Constructor " << "\n";
135
148
  name = new char[80];
136
149
  strcpy(name,obj.name);
137
150
  this->number =obj.number;
138
151
  this->salary = obj.salary;
139
-
140
152
  };
141
153
 
154
+ Worker::~Worker(){
155
+ delete[] name;
156
+ cout << "デコンストラクタ" << "\n";
157
+ }
158
+
159
+ /*
160
+ void ShowData(Worker w2){
161
+ cout << "name = " << w2.name << "\n";
162
+ strcpy(w2.name,"AVD");
163
+ cout << "name = " << w2.name << "\n";
164
+ cout << "number = " << w2.number << "\n";
165
+ cout << "salary = " << w2.salary << "\n";
166
+ }
167
+ */
168
+ int main(){
169
+ Worker w1;
170
+ strcpy(w1.name,"Takayuki");
171
+ w1.number =10;
172
+ w1.salary = 200;
173
+ cout << " Before shwodata" << "\n";
174
+ //ShowData(w1);
175
+ cout << " After shwodata" << "\n";
176
+
177
+ return 0;
178
+ }
179
+
142
180
  ```
143
181
 
144
182
  ###補足情報(言語/FW/ツール等のバージョンなど)