I have some c++ code for a platformer game. I'm trying to make the constructor for a platform object which can have different materials. I've decided to store these materials as enums but when trying to make the enum an argument in the constructor i have some problems.
in Platform.h
[code]
#include "vect2d.h"
#include "Object2D.h"
#pragma once
class Platform : public Object2D
{
private:
public:
enum material { DIRT, SNOW, ICE, ROCK, WOOD };
Platform(vect2d c, int heal, float vY, float vX, float l, float h, material m);
};
[code]
#include "Platform.h"
#include <math.h>
#include "GL\freeglut.h"
Platform::Platform(vect2d c, int heal, float vY, float vX, float l, float h, material m) : Object2D(c, heal, vY, vX, l, h)
{
this->material = m;
}
[/code]
the problem is that i get the error "type name is not allowed" when trying to set this->material in the constructor.
>>276950
>some problems
elaborate, give the compiler output
also:
pragma once should be at the top and public member variables are a bad idea.
>>276950
I think the default type for an enum is int. I'm pretty sure you should have the enum in some scope accessible to wherever the class is instantiated, and pass an int into the constructor, using the name of something in the enum.
so
[code]
enum Materials
{
MAT_DIRT,
MAT_SNOW,
MAT_ICE,
MAT_ROCK,
MAT_WOOD
}
class Something
{
public:
Something(int material);
private:
int material;
};
int main()
{
Something something(MAT_WOOD);
return 0;
}
[/code]
>>277806
as for where you should put the enum, I have no fucking idea. maybe have an 'enums.h' file or something but that could be shit idk.
material is a type. So when you say "material = 1" it's like saying "int = 1", which hopefully even a moron like you knows is wrong. You need to add a class member of type material, like "material mat", and then you should be saying "this->mat = m".