When declaring get_color and get_size, my compiler gives me errors, and I don't know how to solve them. It's probably something really simple, but I think the more I try at it, the more messed up my code becomes (hence the 'this' and '->' references)

marble.cpp(11) : error C2143: syntax error : missing ';' before 'Marble::get_color'
marble.cpp(11) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
marble.cpp(12) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
marble.cpp(12) : error C2556: 'int Marble::get_color(void) const' : overloaded function differs only by return type from 'Marble::Color Marble::get_color(void) const'
marble.h(35) : see declaration of 'Marble::get_color'
marble.cpp(12) : error C2371: 'Marble::get_color' : redefinition; different basic types
marble.h(35) : see declaration of 'Marble::get_color'
marble.cpp(15) : error C2146: syntax error : missing ';' before identifier 'get_size'
marble.cpp(15) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
marble.cpp(16) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
marble.cpp(17) : error C2065: 'size' : undeclared identifier

//Shortened Header File
#ifndef MARBLE_H_
#define MARBLE_H_
include <iostream>
using namespace std;

class Marble
{
public:
enum Color {red, blue, yellow, green};
enum Size {small, medium, large};
private:
Color color;
Size size;
int random_color();
int random_size();
public:
Marble(); // -- random color and size
Marble(Color c); // -- given color, random size
Marble(Size s); //-- random color, given size
Color get_color() const;
Size get_size() const;
};
#endif

Full Header File

//Shortened CPP File

#include "std_lib_facilities.h"
#include <ctime>
#include "marble.h"

const string lcolor[4] = {"red","blue","green","yellow"};
const string lsize[3] = {"small","medium","large"};

Color Marble::get_color() const
{
return (Marble::Color)color;
}
Size get_size()
{
return (Marble::Size)size;
}
int Marble::random_color()
{
return (rand()%4);
}
int Marble::random_size()
{
return ((rand()%3));
}

Marble::Marble()
{
color = (Color)this->random_color();
size = (Size)this->random_size();
}

Full CPP File

The compiler doesn't recognize the type Color. How about Marble::Color? I don't know my C++ anymore...

wow... thanks! haha.

put the enums outside of the class. there's no reason they should be in there...

Also, no need to cast at return (Marble::Color)color; .

put the enums outside of the class. there's no reason they should be in there...

Yes there is -- they're not general enumerations of color and general enumerations of size, they're specific to what kinds of marbles you can have, so it's reasonable to have them namespaced inside Marble.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.