前提・実現したいこと
CS dojoさんの動画をみて同じコードをC++で書いてみたのですが上手くいきません。どうすれば良いのでしょうか?
PersonのクラスでRobotのクラスの変数を宣言したいです。
動画は以下のリンクから見ることができます。
https://www.youtube.com/watch?v=4dqlSk_RPmI&list=PLBZBJbE_rGRV8D7XZ08LK6z-4zPoWzu5H&index=4
javaのサンプルコードだと以下のようになります。
// This class is just for testing Robot and Person. // Note that this class has the same name as this file. public class TestRobotAndPerson { public static void main(String[] args) { Robot r1 = new Robot("Tom", "red", 30); Robot r2 = new Robot("Jerry", "blue", 40); r1.introduceSelf(); r2.introduceSelf(); Person p1 = new Person("Alice", "aggressive", false); Person p2 = new Person("Becky", "talkative", true); p1.robotOwned = r2; p2.robotOwned = r1; p1.robotOwned.introduceSelf(); p2.robotOwned.introduceSelf(); } } // This is the first class we want to study. class Robot { String name; String color; int weight; Robot(String n, String c, int w) { this.name = n; this.color = c; this.weight = w; } void introduceSelf() { System.out.println("My name is " + this.name); } } // This is the second class we want to study. class Person { String name; String personality; boolean isSitting; Robot robotOwned; Person(String n, String p, boolean i) { this.name = n; this.personality = p; this.isSitting = i; } void sitDown() { this.isSitting = true; } void standUp() { this.isSitting = false; } }
発生している問題・エラーメッセージ
test.cpp: In constructor 'Person::Person(std::string, std::string, bool)': test.cpp:27:48: error: no matching function for call to 'Robot::Robot()' 27 | Person (std::string n,std::string p,bool i){ | ^ test.cpp:9:5: note: candidate: 'Robot::Robot(std::string, std::string, int)' 9 | Robot (std::string n,std::string c,int w){ | ^~~~~ test.cpp:9:5: note: candidate expects 3 arguments, 0 provided test.cpp:3:7: note: candidate: 'Robot::Robot(const Robot&)' 3 | class Robot { | ^~~~~ test.cpp:3:7: note: candidate expects 1 argument, 0 provided test.cpp:3:7: note: candidate: 'Robot::Robot(Robot&&)' test.cpp:3:7: note: candidate expects 1 argument, 0 provided
該当のソースコード
#include<iostream> class Robot { public: std::string name; std::string color; int weight; Robot (std::string n,std::string c,int w){ name = n; color = c; weight = w; } void introduce(){ std::cout << "My name is "; std::cout << name << std::endl; } }; class Person { public: std::string name; std::string persoality; bool isSitting; Robot robotOwned; Person (std::string n,std::string p,bool i){ name = n; persoality = p; isSitting = i; } void sitDown(){ isSitting = true; } void standUp(){ isSitting = false; } }; int main(){ Robot r1 = Robot("Tom","red",30); Robot r2 = Robot("Jerry", "blue", 40); r1.introduce(); r2.introduce(); Person p1 = Person("Alice", "aggressive", false); Person p2 = Person("Becky", "talkative", true); p1.robotOwned = r2; p2.robotOwned = r1; p1.robotOwned.introduce(); p2.robotOwned.introduce(); }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/06/07 08:36