Rumah  >  Soal Jawab  >  teks badan

c++11 - xcode编译c++代码的问题

这是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;
}
黄舟黄舟2764 hari yang lalu568

membalas semua(0)saya akan balas

Tiada jawapan
  • Batalbalas