Rumah > Soal Jawab > teks badan
这是cplusplus.com tutorial中的一段代码,但是xcode报错,报错的位置是:baz = new Rectangle[2] { {2,5}, {3,6} };no matching constructor for initialization of 'Rectangle'. 查阅这是c++11的新特性,于是terminal下使用
clang++ -std=c++11 -stdlib=libc++ -Weverything main.cpp
进行编译 同样报错同样的位置,请问这是什么原因?
#include <iostream>
using namespace std;
class Rectangle {
int width, height;
public:
Rectangle(int x, int y) : width(x), height(y) {}
int area(void) { return width * height; }
};
int main() {
Rectangle obj (3, 4);
Rectangle * foo, * bar, * baz;
foo = &obj;
bar = new Rectangle (5, 6);
baz = new Rectangle[2] { {2,5}, {3,6} };
cout << "obj's area: " << obj.area() << '\n';
cout << "*foo's area: " << foo->area() << '\n';
cout << "*bar's area: " << bar->area() << '\n';
cout << "baz[0]'s area:" << baz[0].area() << '\n';
cout << "baz[1]'s area:" << baz[1].area() << '\n';
delete bar;
delete[] baz;
return 0;
}