C ++没有适当的默认构造函数 [英] C++ no appropriate default constructor available
问题描述
我有一些C#的经验,但C ++语法和程序构造产生一些问题。
我使用Visual C ++ 2008.首先为什么会出现这个错误:
1> ...... \\ \\ Form1.h(104):错误C2512:
'Cargame :: Car':没有适当的默认构造函数
其次,为什么这条线不可能? // System :: Drawing :: Color color;
错误C3265:不能在非托管'Car' / p>
Form1.h包含:
namespace Cargame {
using namespaces bla bla bla
class Car;
public ref class Form1:public System :: Windows :: Forms :: Form
{
public:
Form1(void)
{
InitializeComponent();
}
汽车*
protected:
〜Form1()
{
if(components)
{delete components; }
}
更多的自动生成代码
private:System :: Void Form1_Load(System :: Object ^ sender,System :: EventArgs ^ e){
panel1-> BackColor = System :: Drawing :: Color :: Green;
car = new Car();
// car-> draw();
}
};
}
Car.h的内容:
class Car
{
private:
int speed;
// System :: Drawing :: Color color;
public:
Car();
};
Car.cpp的内容
#includestdafx.h
pre>
#includeCar.h
#includeForm1.h
#include< math.h>
// extern TForm1 * Form1;
Car :: Car()
{
speed = 0;
}
void Car :: draw()
{
//实现
}
解决方案非托管代码错误是因为你声明了一个非托管指针。
尝试
Car ^ car
我认为这是正确的语法。
将您的类定义为
ref class Car
I have some experience with C# but C++ syntax and program construction makes some problems. I am using Visual C++ 2008. Firstly why is there this error?:
1>......\Form1.h(104) : error C2512: 'Cargame::Car' : no appropriate default constructor available
Secondly, why is not this line possible? //System::Drawing::Color color;
error C3265: cannot declare a managed 'color' in an unmanaged 'Car'
Form1.h contains:
namespace Cargame { using namespaces bla bla bla class Car; public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); } Car* car; protected: ~Form1() { if (components) { delete components; } } SOME MORE AUTOMATICALLY GENERATED CODE private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { panel1->BackColor = System::Drawing::Color::Green; car = new Car(); //car->draw(); } }; }
Contents of Car.h:
class Car { private: int speed; //System::Drawing::Color color; public: Car(); };
Contents of Car.cpp
#include "stdafx.h" #include "Car.h" #include "Form1.h" #include <math.h> //extern TForm1 *Form1; Car::Car() { speed = 0; } void Car::draw() { //implementation }
解决方案The unmanaged code error is because you declared a unmanaged pointer, I think.
Try
Car ^ car
I think that is the right syntax.And you need to define your class as
ref class Car
这篇关于C ++没有适当的默认构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!