Unqualified ID error, please assist!

Hey all, this forum has been such great help thank you! I am getting the following errors for this code:

26:2 error expected unqualified ID before return
28:1 error expected ";" after class definition


and, if I put a semicolon after class Dice as the second error said (but I thought was not necessary) I get the following error

14:6 expected unqualified id before "{" token

Any help would be great! thanks!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctime>
#ifndef die_h
#define die_h


using namespace std;


class Dice
{
private:
	int sides;
	int value;
	
public:
	Dice(int=6);
	void cast();
	int getSides();
	int getvalue();


	return 0;

}

#endif


Why do you have a return statement inside your class Dice definition?
If you are using header guards, it is a better practice to enclose all includes and declarations in it. It is also considered poor practice to have a using statement in a header file and is more common place in a source file. Honestly there is no need for it as there is nothing in that declaration requiring std::. Classes are how you make your own data types so they don't need to return anything at the end of the declaration. The class functions work like regular functions so those expecting a value to be returned will obviously need return statements at the end of their definitions. So your file should be:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef DIE_H // I prefer capping file name others prefer using #pragma once instead
#define DIE_H
// don't really need the headers here if you don't use them until the source file, but can still have them
// if you just want to have them here instead of the source file
// also, if using C++11 you should try to use the C++ variants of the headers
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <ctime>

class Die{
            int sides;  // I prefer to use the private default and declare the variables at top
            int value; // others prefer to do public functions at top and then private at bottom
      public:
            Dice(int);
            void cast();
            int getSides() const; // prevent the function from changing sides
            int getValue() const;
};

#endif 
Last edited on
Topic archived. No new replies allowed.