c++11 - xcode编译c++代码的问题
黄舟
黄舟 2017-04-17 15:26:46
0
0
588

这是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;
}
黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

Antworte allen(0)
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage