2012-03-12 84 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

相關問題