2012-03-12 53 views
2

我对C#有一些经验,但C++语法和程序构造会造成一些问题。 我使用Visual C++ 2008首先为什么会出现这种错误?:C++没有适当的默认构造函数

1> ...... \ Form1.h(104):错误C2512: 'Cargame ::汽车':无适当的默认构造函数可用

其次,为什么不可能这条线? // System :: Drawing :: Color color;

错误C3265:不能声明在非托管 '汽车' 有管理的 '颜色'

Form1.h包含:Car.h的

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(); 
      } 
    }; 
} 

内容:

class Car 
{ 
private: 
     int speed; 
     //System::Drawing::Color color; 

public: 
     Car(); 
}; 

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 
} 
+0

这不是C++,对不起。 ('public ref class'...不,绝对不是C++)。你的意思是C++/CLI或其他一些变体? – Arafangion 2012-03-12 00:26:33

+0

我刚刚参加了新建项目中的Windows窗体应用程序...... 我有0经验C++ – 2012-03-12 00:29:49

+0

在哪个版本的Visual Studio中? – Arafangion 2012-03-12 00:32:40

回答

1

要解决错误C2512您需要添加:

#include "Car.h" 

到Form1.h。

+0

这使得关于错误语法的一些错误加上关于C2512的错误.. – 2012-03-12 00:50:21

1

将类Car的定义放置在与其前向声明相同的命名空间中。

例如

内容Car.h的:

namespace Cargame { 
class Car 
{ 
private: 
     int speed; 
     //System::Drawing::Color color; 

public: 
     Car(); 
}; 
} 

Car.cpp

#include "stdafx.h" 
#include "Car.h" 
#include "Form1.h" 
#include <math.h> 

//extern TForm1 *Form1; 
using namespace Cargame; 
Car::Car() 
{ 
     speed = 0; 
} 

void Car::draw() 
{ 
//implementation 
} 
1

的非托管代码错误是因为你声明的非托管指针的内容,我想。

尝试Car^car我认为这是正确的语法。

而且您需要将您的类定义为ref class Car

相关问题