std::ifstream ifile(argv[1]);
std::string input((std::istreambuf_iterator<char>(ifile)),
(std::istreambuf_iterator<char>() ));
Terminal(std::string input):_input(input) {
std::regex re("(\n|\r)+");
input = std::regex_replace(input, re, " ");
}
class Rectangle: public Shape {
public:
Rectangle(std::string id, double length, double width): Shape(id), _length(length), _width(width) {
}
class Shape {
protected:
string _id;
string _color;
public:
Shape(string id): _id(id), _color("white"){};
Shape(string id, string color): _id(id), _color(color){};
}
class Rectangle: public Shape {
protected:
double _width, _height;
public:
Rectangle(string id , double width, double height ): Shape(id), _width(width), _height(height) {
_id=id;
_color="white";
}
Rectangle(string id , double width, double height, string color): Shape(id), Shape(color), _width(width), _height(height) {
_id=id;
_color=color;
}
jerry@LAPTOP-4VVBP80S:/mnt/d/NTUT/POSD/109598061_HW$ make test
g++ -Werror test/ut_main.cpp -o bin/ut_main -lgtest -lpthread
In file included from test/ut_rectangle.h:1:0,
from test/ut_main.cpp:2:
test/../src/rectangle.h: In constructor ‘Rectangle::Rectangle(std::__cxx11::string, double, double, std::__cxx11::string)’:
test/../src/rectangle.h:22:5: error: multiple initializations given for base ‘Shape’
Rectangle(string id , double width, double height, string color): Shape(id), Shape(color), _width(width), _height(height) {
^~~~~~~~~
makefile:15: recipe for target 'ut_main' failed
make: *** [ut_main] Error 1
34 行 Shape(id)
, Shape(color)
,呼叫了兩次Shape
,所以錯誤寫 multiple initializations given for base 'Shape'
.
改寫成Shape(id, color)
即可
下方程式碼不知道該何時丟 exception 以及進入 nested object
void deleteShapeById(std::string id) {
for( std::vector<Shape *>::iterator ptr=_shapes.begin ; ptr<_shapes.end ; ptr++){
if(ptr.id()==id){
_shapes.erase( ptr , ptr+1 );
}
else(ptr.color()=="transparent"){
ptr.deleteShapeById(id);
}
}
}
增加 catch exception 及 throw exception
void deleteShapeById(std::string id) {
for( std::vector<Shape *>::iterator ptr = _shapes.begin ; ptr != _shapes.end ; ptr++){
if(ptr.id() == id) {
_shapes.erase( ptr );
} else {
try {
ptr.deleteShapeById(id);
return;
} catch (std::string e) {
}
}
}
throw "error";
}
ptr 的使用有問題,你自己改
可是你的throw丟在最後面不會有問題嗎?
如果你的Compound_shape={retangle,compound{retangle,ellipse},ellipse},在第二個compound應該就會throw了吧?
沒錯,但是會由上一層的 try block 接住,所以不會有問題
對了,getShapeById
不要 erase 喔!
mkdir -p bin
#mkdir -p obj
g++ -std=c++11 -Wfatal-errors test/ut_main.cpp -o bin/ut_main -lgtest -lpthread
/usr/bin/ld: /tmp/ccOAnkAY.o: in function `Shape::Shape(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
ut_main.cpp:(.text._ZN5ShapeC2ENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN5ShapeC5ENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x27): undefined reference to `vtable for Shape'
/usr/bin/ld: /tmp/ccOAnkAY.o: in function `Shape::~Shape()':
ut_main.cpp:(.text._ZN5ShapeD2Ev[_ZN5ShapeD5Ev]+0x13): undefined reference to `vtable for Shape'
/usr/bin/ld: /tmp/ccOAnkAY.o:(.data.rel.ro._ZTI8Triangle[_ZTI8Triangle]+0x10): undefined reference to `typeinfo for Shape'
/usr/bin/ld: /tmp/ccOAnkAY.o:(.data.rel.ro._ZTI7Ellipse[_ZTI7Ellipse]+0x10): undefined reference to `typeinfo for Shape'
/usr/bin/ld: /tmp/ccOAnkAY.o:(.data.rel.ro._ZTI9Rectangle[_ZTI9Rectangle]+0x10): undefined reference to `typeinfo for Shape'
collect2: error: ld returned 1 exit status
make: *** [makefile:12: ut_main] Error 1
class Shape
{
private:
string _id, _color;
public:
Shape(std::string id) : _id(id), _color("white"){}; // interface for default color "white".
Shape(std::string id, std::string color) : _id(id), _color(color){}; // interface for color input by user.
std::string id() const; // return id of shape.
std::string color() const; // return color of shape.
virtual double area() const = 0;
virtual double perimeter() const = 0;
virtual string info() const = 0;
virtual void addShape(Shape *shape); // throw std::string "Only compound shape can add shape!"
virtual void deleteShapeById(std::string id); // throw std::string "Only compound shape can delete shape!"
virtual Shape *getShapeById(std::string id); // throw std::string "Only compound shape can get shape!"
~Shape(){};
};
我才剛開始,還沒建compound shape
只在shape裡面加了這次要改的virtual function跟建構元
以及改了Rectangle Ellipse Triangle建構元
還有unit test的呼叫部分,呼叫的參數加了"0",就醬
這是改過的Rectangle建構元
Rectangle(string id, double length, double width) : Shape(id), _length(length), _width(width)
{
if (length <= 0 || width <= 0)
throw string("This is not a rectangle!");
// no need
//_id = id;
//_color = "white";
}
Rectangle(string id, double length, double width, string color) : Shape(id, color), _length(length), _width(width)
{
// If the rectangle can't be successfully created,
// handle the exception by throwing string "This is not a rectangle!"
if (length <= 0 || width <= 0)
throw string("This is not a rectangle!");
//_length = length;
//_width = width;
//_id = id;
//_color = color;
}