Reputation: 780
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
}
Upvotes: 2
Views: 2638
Reputation: 54355
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
Upvotes: 1
Reputation: 2914
Place the definition of a class Car
in the same namespace as its forward declaration has been placed.
e.g.
Contents of Car.h:
namespace Cargame {
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;
using namespace Cargame;
Car::Car()
{
speed = 0;
}
void Car::draw()
{
//implementation
}
Upvotes: 1
Reputation: 4348
To resolve error C2512 you need to add:
#include "Car.h"
to Form1.h.
Upvotes: 1