###前提・実現したいこと
現在、C++でクラスを定義しゲッターとセッターを作成しました。
またCompanyNameという、ヘッダーファイルで定義したstatic変数を付けたのですが、エラーが発生し、実行できません。ポインタの使い方がおかしいと思うのですが、サンプルプログラムを自分なりにアレンジして作成したので、このプログラムの中でポインタを使う理由もあまり分かりません。またなぜ、C++ではStringを使わず、文字列を毎回をポインタにして使用しているのでしょうか。
###発生している問題・エラーメッセージ
Main.cpp:9:15: error: redefinition of 'ComapanyName' with a different type: 'char ' vs 'char [80]'
char Worker::ComapanyName = 'ABC';
^
./Worker.h:3:18: note: previous definition is here
static char ComapanyName[80];
^
Main.cpp:9:30: warning: multi-character character constant [-Wmultichar]
char* Worker::ComapanyName = 'ABC';
^
Main.cpp:13:30: warning: multi-character character constant [-Wmultichar]
strcpy(this->department, 'NotDefinedDepartment');
^
Main.cpp:13:30: warning: character constant too long for its type
Main.cpp:13:5: error: no matching function for call to 'strcpy'
strcpy(this->department, 'NotDefinedDepartment');
^~~~~~
/usr/include/string.h:129:14: note: candidate function not viable: no known conversion from 'char *[80]' to 'char *restrict' for 1st argument
extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
^
Main.cpp:14:24: warning: multi-character character constant [-Wmultichar]
strcpy(this->name, 'NotDefinedName');
^
Main.cpp:14:24: warning: character constant too long for its type
Main.cpp:14:5: error: no matching function for call to 'strcpy'
strcpy(this->name, 'NotDefinedName');
^~~~~~
/usr/include/string.h:129:14: note: candidate function not viable: no known conversion from 'char *[80]' to 'char *restrict' for 1st argument
extern char *strcpy (char __restrict __dest, const char __restrict __src)
^
Main.cpp:23:9: error: out-of-line definition of 'Worker' does not match any declaration in 'Worker'
Worker::Worker(int number, char department,char name, double salary){
^~~~~~
Main.cpp:25:5: error: no matching function for call to 'strcpy'
strcpy(this->department, department);
^~~~~~
/usr/include/string.h:129:14: note: candidate function not viable: no known conversion from 'char *[80]' to 'char *restrict' for 1st argument
extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
^
Main.cpp:26:5: error: no matching function for call to 'strcpy'
strcpy(this->name, name);
^~~~~~
/usr/include/string.h:129:14: note: candidate function not viable: no known conversion from 'char *[80]' to 'char *restrict' for 1st argument
extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
^
Main.cpp:34:43: error: expected ';' after expression
cout << "department = " << *department
^
;
Main.cpp:48:5: error: no matching function for call to 'strcpy'
strcpy(this->name, name);
^~~~~~
/usr/include/string.h:129:14: note: candidate function not viable: no known conversion from 'char *[80]' to 'char *restrict' for 1st argument
extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
^
Main.cpp:52:12: error: cannot initialize return object of type 'char *' with an lvalue of type 'char *[80]'
return this->name;
^~~~~~~~~~
5 warnings and 9 errors generated.
###ソースコード
ヘッダーファイル<Worker.h>
using namespace std;
int Worker::num=0;
char* Worker::ComapanyName = 'ABC';
Worker::Worker(){
int number = num;
strcpy(this->department, 'NotDefinedDepartment');
strcpy(this->name, 'NotDefinedName');
salary = 0;
num++;
}
Worker::~Worker(){
cout << "This is ~Worker();" << "\n";
}
Worker::Worker(int number, char* department,char* name, double salary){
this->number = number;
strcpy(this->department, department);
strcpy(this->name, name);
this->salary = salary;
}
void Worker::ShowData(){
cout << "number = " << number << "\n";
cout << "company = " << ComapanyName << "\n";
cout << "department = " << *department
cout << "name = " << *name << "\n";
cout << "salary = " << salary << "\n";
}
void Worker::setNumber(int number){
this->number = number;
}
int Worker::getNumber(){
return this->number;
}
void Worker::setName(char* name){
strcpy(this->name, name);
}
char* Worker::getName(){
return this->name;
}
void Worker::setSalary(double salary){
this->salary = salary;
}
double Worker::getSalary(){
return this-> salary;
}
int main(void){
return 0;
}
メイン <Main.cpp>
using namespace std;
int Worker::num=0;
char* Worker::ComapanyName = 'ABC';
Worker::Worker(){
int number = num;
strcpy(this->department, 'NotDefinedDepartment');
strcpy(this->name, 'NotDefinedName');
salary = 0;
num++;
}
Worker::~Worker(){
cout << "This is ~Worker();" << "\n";
}
Worker::Worker(int number, char* department,char* name, double salary){
this->number = number;
strcpy(this->department, department);
strcpy(this->name, name);
this->salary = salary;
}
void Worker::ShowData(){
cout << "number = " << number << "\n";
cout << "company = " << ComapanyName << "\n";
cout << "department = " << *department
cout << "name = " << *name << "\n";
cout << "salary = " << salary << "\n";
}
void Worker::setNumber(int number){
this->number = number;
}
int Worker::getNumber(){
return this->number;
}
void Worker::setName(char* name){
strcpy(this->name, name);
}
char* Worker::getName(){
return this->name;
}
void Worker::setSalary(double salary){
this->salary = salary;
}
double Worker::getSalary(){
return this-> salary;
}
int main(void){
return 0;
}
###補足情報(言語/FW/ツール等のバージョンなど)
paiza.io
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/01/19 05:29