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

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

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

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

Q&A

1回答

388閲覧

クラスの継承でメンバ関数が呼び出せない

Cacciatello

総合スコア8

C++

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

0グッド

0クリップ

投稿2018/05/24 07:17

前提・実現したいこと

長方形をcanvasを使って描きたい。
(点、幅、高さをメンバ変数として。

プログラムファイルは全部で5つあります。

rect(オブジェクト)を宣言してメンバ関数を呼び出したい
(メンバ関数名が重なっていてできない?)

発生している問題・エラーメッセージ

expected ';' before 'rect' メソッド'draw'を解決できませんでした 'rect' was not declared in this scope

該当のソースコード

C++

1//ex.cpp 2#include <iostream> 3#include "canvas/canvas.h" 4#include "Rectangle.h" 5#include "ColoredRectangle.h" 6 7using namespace canvas; 8using namespace std; 9 10int main(){ 11 show(); 12 Rectangle rect(100,100,50,50);//エラー箇所 13 rect.draw();//エラー箇所 14 ColoredRectangle crect(200,200,50,50,255,0,0); 15 crect.draw(); 16 waitForKey(); 17} 18------------------------- 19//Rectangle.h 20#include <string> 21#include <sstream> 22#include "Point.h" 23#include "canvas/canvas.h" 24 25class Rectangle{ 26protected: 27 Point p; 28 int width; 29 int height; 30public: 31 Rectangle(int x,int y,int w,int h): 32 p(Point(x,y)),width(w),height(h){} 33 int getX()const{return p.x;} 34 int getY()const{return p.y;} 35 int getWidth()const {return width;} 36 int getHeight()const{return height;} 37 38 void setX(int x){p.x = x;} 39 void setY(int y){p.y = y;} 40 void setWidth(int w){width = w;} 41 void setHeight(int h){height = h;} 42 43 void draw()const{ 44 canvas::setColor(0,0,0); 45 canvas::drawRectangle(p.x,p.y,width,height); 46 } 47 std::string toString()const{ 48 std::stringstream ss; 49 ss<<"Rectangle[leftTop="<<p<<",width = "<<width<<",height = "<<height<<"]"; 50 return ss.str(); 51 } 52}; 53 54--------------------------- 55//ColoredRectangle.h 56#include "Color.h" 57 58class ColoredRectangle:public Rectangle{ 59 Color color; 60public: 61 ColoredRectangle(int x,int y,int w,int h, 62 int r,int g,int b): 63 Rectangle(x,y,w,h),color(r,g,b){} 64 65 Color getColor()const{return color;} 66 void setColor(const Color& col){color = col;} 67 void setColor(int red,int green,int blue){ 68 color = Color(red,green,blue); 69 } 70 71 void draw()const{ 72 canvas::setColor(color.r,color.g,color.b); 73 canvas::drawRectangle(p.x,p.y,width,height); 74 } 75 std::string toString()const{ 76 std::stringstream ss; 77 ss<<"ColoredRectangle[leftTop = "<<p<<",width = " 78 <<width<<",height = "<<height<<",color = "<<color<<"]"; 79 return ss.str(); 80 } 81}; 82---------------------- 83 84//Color.h 85#include <string> 86#include <sstream> 87 88class Color{ 89public: 90 int r; 91 int g; 92 int b; 93 Color(int red,int green,int blue): 94 r(red),g(green),b(blue){} 95 std::string toString()const{ 96 std::stringstream ss; 97 ss<<"Color["<<r<<","<<g<<","<<b<<"]"; 98 return ss.str(); 99 } 100}; 101 102 103inline std::ostream& operator<<(std::ostream& s,const Color& color){ 104 return s<<color.toString(); 105} 106------------------------ 107 108//Point.h 109#include <string> 110#include <sstream> 111 112class Point{ 113public: 114 int x; 115 int y; 116 Point(int xp=0,int yp=0):x(xp),y(yp){} 117 std::string toString()const{ 118 std::stringstream ss; 119 ss<<'('<<x<<","<<y<<')'; 120 return ss.str(); 121 } 122}; 123 124 125inline std::ostream& operator<<(std::ostream& s,const Point& point){ 126 return s<<point.toString(); 127} 128------------------- 129 130 131 132```

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

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

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

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

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

guest

回答1

0

こんにちは。

パッと見る限り問題ないように見えます。
プリコンパイルヘッダの罠などに落ちているかも知れません。リビルドしてみてもダメでしょうか?
リビルドでもダメなら、問題がでる最小のソースを作ってみて下さい。恐らくその過程で解決するだろうと思いますが、もし、解決しなかったら、そのコードを上げて貰えれば何かアドバイスできると思います。

投稿2018/05/24 07:28

Chironian

総合スコア23272

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問