前提・実現したいこと
このプログラムで、#pragma onceを付けなかった場合に複数回重複includeされてしまうのは何故でしょうか。
Compilation error
In file included from main.cpp:2:0: shape.h:4:7: error: redefinition of 'class Shape' class Shape{ ^~~~~ In file included from main.cpp:1:0: shape.h:4:7: note: previous definition of 'class Shape' class Shape{ ^~~~~ In file included from main.cpp:2:0: shape.h:11:7: error: redefinition of 'class Rectangle' class Rectangle : public Shape{ ^~~~~~~~~ In file included from main.cpp:1:0: shape.h:11:7: note: previous definition of 'class Rectangle' class Rectangle : public Shape{ ^~~~~~~~~ In file included from main.cpp:2:0: shape.h:18:7: error: redefinition of 'class Circle' class Circle : public Shape{ ^~~~~~ In file included from main.cpp:1:0: shape.h:18:7: note: previous definition of 'class Circle' class Circle : public Shape{ ^~~~~~ In file included from main.cpp:2:0: shape.h:25:7: error: redefinition of 'float Rectangle::getArea()' float Rectangle::getArea(){ ^~~~~~~~~ In file included from main.cpp:1:0: shape.h:25:7: note: 'virtual float Rectangle::getArea()' previously defined here float Rectangle::getArea(){ ^~~~~~~~~ In file included from main.cpp:2:0: shape.h:28:7: error: redefinition of 'float Rectangle::getPerim()' float Rectangle::getPerim(){ ^~~~~~~~~ In file included from main.cpp:1:0: shape.h:28:7: note: 'virtual float Rectangle::getPerim()' previously defined here float Rectangle::getPerim(){ ^~~~~~~~~ In file included from main.cpp:2:0: shape.h:31:7: error: redefinition of 'float Circle::getArea()' float Circle::getArea(){ ^~~~~~ In file included from main.cpp:1:0: shape.h:31:7: note: 'virtual float Circle::getArea()' previously defined here float Circle::getArea(){ ^~~~~~ In file included from main.cpp:2:0: shape.h:34:7: error: redefinition of 'float Circle::getPerim()' float Circle::getPerim(){ ^~~~~~ In file included from main.cpp:1:0: shape.h:34:7: note: 'virtual float Circle::getPerim()' previously defined here float Circle::getPerim(){ ^~~~~~ make: *** [main.o] Error 1
該当のソースコード(shape.h)
C++
1#pragma once 2#include<iostream> 3#include<cmath> 4using namespace std; 5class Shape{ //virtual base class 6 public: 7 virtual float getArea() = 0; //pure virtual function 8 virtual float getPerim() = 0; //pure virtual function 9 protected: 10 float w, h, r; 11}; 12class Rectangle : virtual public Shape{ //derived class 13 public: 14 Rectangle(){} 15 Rectangle(const float& width,const float& height){w = width; h = height;} 16 float getArea(); 17 float getPerim(); 18}; 19class Circle : virtual public Shape{ //derived class 20 public: 21 Circle(){} 22 Circle(const float& radius){r = radius;} 23 float getArea(); 24 float getPerim(); 25}; 26float Rectangle::getArea(){ //override 27 return w * h; 28} 29float Rectangle::getPerim(){ //override 30 return (w + h) * 2; 31} 32float Circle::getArea(){ //override 33 return r * r * acos(-1); 34} 35float Circle::getPerim(){ //override 36 return 2 * r * acos(-1); 37}
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。