【发布时间】:2014-06-23 00:29:29
【问题描述】:
#include <iostream>
#include <stdlib.h>
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;
system("PAUSE");
return 0;
}
此代码在代码块中执行,但在 Visual Studio 中,没有可用的适当默认构造函数。
我尝试在这里搜索类似的东西,但我找不到任何东西。
【问题讨论】:
-
您没有默认构造函数。如果您发布整个错误并显示它抱怨的行,您可能会得到答案。
-
baz = new Rectangle[2] { {2,5}, {3,6} };可能会给您一些关于默认构造函数的编译器问题。 -
您拥有哪个版本的 Visual Studio 以及您将哪个编译器/版本与代码块一起使用(代码块不是编译器;它是一个开发环境,可以与不同的编译器一起使用)?
-
另外,当询问涉及编译器错误的问题时,请准确地引用错误。
标签: c++ arrays class pointers default-constructor