###前提・実現したいこと
関数の戻り値をポンタに指定し、下記のように行っています。
しかし
w1.ShowDataPointer(&w2);
を実行したところエラーが発生しました。
解決方法が分からないので、教えて頂けたら幸いです。
w2 = w1.inputdata();
w1.ShowDataPointer(&w2);
###発生している問題・エラーメッセージ
Main.cpp:10:29: warning: ISO C++11 does not allow conversion from string literal to 'char ' [-Wwritable-strings]
char Worker::CompanyName = "ABC";
^
Main.cpp:55:13: warning: address of stack memory associated with local variable 'obj' returned [-Wreturn-stack-address]
return &obj;
^~~
Main.cpp:73:22: error: cannot initialize a parameter of type 'Worker *' with an rvalue of type 'Worker **'
w1.ShowDataPointer(&w2);
^~~
Main.cpp:39:38: note: passing argument to parameter 'a' here
void Worker::ShowDataPointer(Worker *a){
^
2 warnings and 1 error generated.
###ソースコード
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(); void ShowDataPointer(Worker *a); Worker* inputdata(); }; メイン文 #include <iostream> using namespace std; #include <iostream> #include "string.h" #include "Worker.h" using namespace std; int Worker::no = 0; char* Worker::CompanyName = "ABC"; Worker::Worker(){ this->num = no; 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->num << "\n"; cout << "salary = " << this->salary << "\n"; cout << "department = " << this->department << "\n"; cout << "name = " << this->name << "\n"; } void Worker::ShowDataPointer(Worker *a){ cout << "no = " << a->num << "\n"; cout << "salary = " << a->salary << "\n"; cout << "department = " << a->department << "\n"; } Worker* Worker::inputdata(){ Worker obj; obj.num = no; obj.salary=100; strcpy(obj.department, "inputdata"); strcpy(obj.name, "name_inputdata"); no++; cout << "this is inputdata" << "\n"; obj.ShowData(); cout << "this is end of inputdata" << "\n"; return &obj; } int main(void){ Worker w[3]; Worker w1; Worker* w2; 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 << "salary = " << someone[i]->salary << "\n"; } w2 = w1.inputdata(); w1.ShowDataPointer(&w2); cout << "---------w2.ShowData();------------" << "\n"; w2->ShowData(); cout << "---------------------" << "\n"; w1.ShowData(); w[0].ShowData(); cout << "---------------------" << "\n"; w[1].ShowData(); cout << "---------------------" << "\n"; w[0].ShowDataPointer(&w[0]); }
###補足情報(言語/FW/ツール等のバージョンなど)
paiza.io
回答2件
あなたの回答
tips
プレビュー