###前提・実現したいこと
配列で定義されたポインタで、各要素ごとの配列を管理したいと考えております。
下記のようにすることは不可能でしょうか?
someone[i] = w[i];
cout << someone[i].salary << "\n";
###発生している問題・エラーメッセージ
Main.cpp:7:29: warning: ISO C++11 does not allow conversion from string literal to 'char ' [-Wwritable-strings]
char Worker::CompanyName = "ABC";
^
Main.cpp:10:8: warning: expression result unused [-Wunused-value]
this->num;
Main.cpp:43:18: error: assigning to 'Worker *' from incompatible type 'Worker'; take the address with & someone[i] = w[i]; ^ ~~~~ & Main.cpp:44:25: error: member reference type 'Worker *' is a pointer; maybe you meant to use '->'? cout << someone[i].salary << "\n"; ~~~~~~~~~~^ -> ###ソースコード ```<メイン文> #include <iostream> #include "string.h" #include "Worker.h" using namespace std; int Worker::no = 0; char* Worker::CompanyName = "ABC"; Worker::Worker(){ this->num; cout << "-------------------" << Worker::no << "\n"; this->salary = 0; strcpy(this->department, "NotDefinedDepartment"); strcpy(this->name, "NotDefinedName"); no++; } Worker::~Worker(){ } Worker::Worker(double salary ,char department[100], char name[100]){ this->num = no; this->salary = salary; strcpy(this->department,department); strcpy(this->name,name); no++; } void Worker::ShowData(){ cout << "no = " << this->no << "\n"; cout << "salary = " << this->salary << "\n"; cout << "department = " << this->department << "\n"; cout << "name = " << this->name << "\n"; } int main(void){ Worker w[3]; Worker* someone[3]; for(int i=0 ; i<3 ;i++){ w[i].salary = 100; strcpy(w[i].department, "E"); strcpy(w[i].name, "Takayuki"); someone[i] = w[i]; cout << someone[i].salary << "\n"; } w[0].ShowData(); w[1].ShowData(); } <ヘッダー文> class Worker{ public: static int no; int num; static char* CompanyName; double salary; char department[100]; char name[100]; Worker(); ~Worker(); Worker(double salary ,char department[100], char name[100]); void ShowData(); }; ``` ###補足情報(言語/FW/ツール等のバージョンなど) paiza.io
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/01/20 00:58