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

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

新規登録して質問してみよう
ただいま回答率
85.50%
オブジェクト指向

オブジェクト指向プログラミング(Object-oriented programming;OOP)は「オブジェクト」を使用するプログラミングの概念です。オブジェクト指向プログラムは、カプセル化(情報隠蔽)とポリモーフィズム(多態性)で構成されています。

C++

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

Q&A

解決済

1回答

9149閲覧

C++ のクラスの使用とstaticとグローバル変数の使い方

saito.kaz

総合スコア76

オブジェクト指向

オブジェクト指向プログラミング(Object-oriented programming;OOP)は「オブジェクト」を使用するプログラミングの概念です。オブジェクト指向プログラムは、カプセル化(情報隠蔽)とポリモーフィズム(多態性)で構成されています。

C++

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

0グッド

0クリップ

投稿2016/01/19 04:31

###前提・実現したいこと
現在、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

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

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

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

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

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

guest

回答1

0

ベストアンサー

こんにちは。

またなぜ、C++ではStringを使わず、文字列を毎回をポインタにして使用しているのでしょうか。

std::stringを使っていない理由は不明ですが、そのサンプルの作者がstd::stringを知らなかったのかもしれません。C++をベターCとして使う人も少なくないですし。(かく言う私も一昨年までその一人でした。)


エラーの意味を少し解説してみます。

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行目とWorker.hの3行目で、CompanyNameの定義が異なります。
前者はchar型へのポインタ、後者は「char型へのポインタ」80個の配列となってます。

Main.cpp:9:30: warning: multi-character character constant [-Wmultichar]

char* Worker::ComapanyName = 'ABC';
^

''で括ることができるのは1文字だけです。結果はchar型のリテラルになります。
3文字括ってはいけません。たぶん、"ABC"と書きたかったのでは?
文字列リテラルは""で括って下さい。
文字リテラルを``で括ります。(文字列でなく文字なので、1文字だけ括れます。)
以下同様なエラーは省略。

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){
^~~~~~

同じコンストラクタがWorker.hでも定義されています。

Main.cpp:25:5: error: no matching function for call to 'strcpy'

strcpy(this->department, department); ^~~~~~

Workerクラス内にdepartmentを定義していないのでは?
以下略。

C言語の基礎を勉強しましょう。C言語の基礎解説はQAサイトの役割ではないと思います。

投稿2016/01/19 04:58

編集2016/01/19 05:25
Chironian

総合スコア23272

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

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

saito.kaz

2016/01/19 05:29

ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問