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

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

新規登録して質問してみよう
ただいま回答率
85.48%
C++

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

Q&A

解決済

1回答

953閲覧

あるクラスで別のクラスの変数を宣言したい

Atsuya_H

総合スコア1

C++

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

0グッド

0クリップ

投稿2020/06/07 08:17

前提・実現したいこと

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(); }

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

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

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

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

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

guest

回答1

0

ベストアンサー

no matching function for call to 'Robot::Robot()'

と言ってるみたいなので、
class Robot に Robot() {} (空っぽのコンストラクタ) を追加してあげれば良いのでは?

投稿2020/06/07 08:33

pepperleaf

総合スコア6383

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

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

Atsuya_H

2020/06/07 08:36

ありがとうございます! 引数が無いコンストラクタが定義されていなかったのですね、、
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問