errors creating my first class

Need some help figuring out how to fix this. I created my first class, and I'm unsure how to stop the error messages. This program is designed to make take a number in Base 10(or the user can select a different base, which changes all the other bases) and then output then number in base 16, base 8 and base 2(but if the main base changes, so do all of the other bases). Haven't been able to even run my program once... I don't want to make static variables, because the user can change them. I'll list errors after code.

**edited 4/5/12 12:15 PM for minor accidental errors***
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <iostream>
using namespace std;

class decimalHexOctalBinary
{
public:

	int decimalUnsignedInt(int unconverted, int baseNumber)
	{ 
		int firstDigit = 0;
		int secondDigit = 0;
		int thirdDigit = 0;
		int fourthDigit = 0;
		int converted = 0;
		while(unconverted > 0)
		{
			fourthDigit++;
			unconverted--;
			if (fourthDigit >= baseNumber)
			{
				for(int j = 0; j < unconverted-1; j++)
				{
					thirdDigit++;
					if(thirdDigit > baseNumber)
					{
						for(int k =0; j < baseNumber; k++)
						{
							secondDigit++;
							if (secondDigit > baseNumber)
							{
								for(int l=0; l<baseNumber; l++)
								{
									firstDigit++;
									break;
								}
							}
							break;
						}//for secondDigit++
					}//thirdDigit
					break;
				}//fourthDigit
			}//if
			break;
		}//while
		
		cout << "The number" << unconverted << " converted to" << baseNumber << " base, is";
			 displayConversion(firstDigit, secondDigit, thirdDigit, fourthDigit);
		
	}
	void displayConversion(int firstDigit, int secondDigit, int thirdDigit, int fourthDigit)
	{
		if (firstDigit == 0)
		{
			if(secondDigit==0)
			{
				if(thirdDigit == 0)
				{
					cout << fourthDigit << endl;
					break;
				}
				cout << thirdDigit << fourthDigit << endl;
				break;
			}
			cout << secondDigit << thirdDigit << fourthDigit << endl;

		}
		else 
			cout << firstDigit << secondDigit << thirdDigit << fourthDigit;

	}// displayConverstion

	int hexadecimalUnsignedInt(int unconverted, int baseNumber)
	{
		int newBaseNumber = baseNumber+6;
		decimalUnsignedInt(unconverted, newBaseNumber);
		return 0;

	}
	int octalUnsignedInt(int unconverted, int baseNumber)
	{
		int newBaseNumber = baseNumber-2;
		decimalUnsignedInt(unconverted, newBaseNumber);
		return 0;

	}

	int binaryUnsignedInt(int unconverted, int baseNumber)
	{
		int newBaseNumber = baseNumber-8;
		decimalUnsignedInt(unconverted, newBaseNumber);
		return 0;

	}
}; // end class

int main()
{
	int unconverted;
	int baseNumber = 10;
	char answer = 'y';

	cout << "The original base number is: Base " << baseNumber << endl;
	cout << "The bases this program will cover are: " << baseNumber-8 << ", "
		<< baseNumber-2 << ", " << baseNumber << ", and " << baseNumber+6 <<  "." << endl;
	
	
	 while(answer == 'y')
	{
		cout << "Would you like change the base? y/n" << endl;
		cin >> answer;
		cout << "What base for the original number base?" << endl;
		cin >> baseNumber;
		if(baseNumber < 0)
			cout << "That number is invalid, please only input positive integers." << endl;
	 }

	cout << "Please enter a value(in base 10) to convert to other bases." << endl;
	cin >> unconverted;

	decimalHexOctalBinary::decimalUnsignedInt(unconverted, baseNumber);
	decimalHexOctalBinary::hexadecimalUnsignedInt(unconverted, baseNumber);
	decimalHexOctalBinary::octalUnsignedInt(unconverted, baseNumber);
	decimalHexOctalBinary::binaryUnsignedInt(unconverted, baseNumber);

	return 0;
}


(58) : error C2043: illegal break [i]//how can I break the loop without break?
(61) : error C2043: illegal break

(119) : error C2352: 'decimalHexOctalBinary::decimalUnsignedInt' : illegal call of non-static member function
-----(8) : see declaration of 'decimalHexOctalBinary::decimalUnsignedInt'
//did a bit of research on this one, should I just make static functions?

(120) : error C2352: 'decimalHexOctalBinary::hexadecimalUnsignedInt' : illegal call of non-static member function
-----(71) : see declaration of 'decimalHexOctalBinary::hexadecimalUnsignedInt'
(121) : error C2352: 'decimalHexOctalBinary::octalUnsignedInt' : illegal call of non-static member function
-----(78) : see declaration of 'decimalHexOctalBinary::octalUnsignedInt'
(122) : error C2352: 'decimalHexOctalBinary::binaryUnsignedInt' : illegal call of non-static member function
-----(86) : see declaration of 'decimalHexOctalBinary::binaryUnsignedInt'
Last edited on
In the method displayConversion(), you're not breaking at a loop! it's an if condition...! break can be used only in loops.

Loops are for, while and do.
Last edited on
decimalHexOctalBinary::decimalUnsignedInt(unconverted, baseNumber);


You've pretty much missed the point of classes. A class is a way for you to define a new kind of object. Then, you can make one of those objects.

Here, this is how to make some objects of type int, and then use them:

1
2
3
4
5
int x;
int y;
int z;
x = 7;
y  = 84;


and here is how to make an object of type decimalHexOctalBinary, and then use it:

1
2
decimalHexOctalBinary x;
x.decimalUnsignedInt(unconverted, baseNumber);



Thank you for your replies:
@TheDestroyer - I see what you mean, haha I can't believe I did that.
@ Moschops or anyone who understands classes - do I make that object of type decimalHexOctalBinary IN the class, or in main int? Thanks
Also, if anyone has a great resource that explains classes well, I'd appreciate a link. I've done ALOT of research online, so I'm looking for buried treasure, not just top 10 results from google.
You make objects where you want to use them. You want to use the object in your main function? Well then make it there.

do I make that object of type decimalHexOctalBinary IN the class,

That makes no sense. You might as well ask if you make an int inside itself.

A class is a kind of object you can create. Just like an int, or a double. You can create one and use one. You can create ten and use ten. You can create five and just use two. Just like an int or a double. The only difference is that int and double are provided for you. Someone else already explained to the compiler how to make them. A class is something that YOU explained to the compiler how to make.

Conceptually, that's all there is to it. Everything else is learning the syntax, and a handful of extra neat things you can do. Don't go any further until you understand that concept.


When you type
1
2
3
4
5
class SOMENEWCLASS{
...
...
...
};

everything inside those braces is you explaining to the compiler how to make this new kind of object.
Last edited on
Thank you very much. As the title states, this is the first class I've made, so help is greatly appreciated. My assignment is due tonight, and I don't really feel like I understand classes very well. I haven't been able to go to school, so all of my code is from online tutorials and other code helping me understand what to do. Thanks so much for your help... hope I can make this work haha.
You should start with this:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

class someNumbers
{
public:
  int x;
  float y;
};

int main()
{
  someNumbers first;
   someNumbers second;

  first.x = 7;
  second.x = 12;

  cout << " first.x = " << first.x << endl;
  cout << " second.x = " << second.x << endl;

  }


and play with the values of x and y in the two instances of the class until you understand what's going on. The point of classes is to be able to make lots of them, and they all hold their individual values; your first class above is just a function store. You would never need more than one instance of that class, and putting all those functions into it doesn't make much sense when you could just have them as functions. That's not to say there's no use for such a thing, but as a first class to learn the concept of classes with, it misses the point completely.
Last edited on
Topic archived. No new replies allowed.