-
Classes won't compile
Hi all!
I was just wondering if anyone could figure out why this won't compile... I grant you main does nothing right now, but the code should still compile (I think!)
For every class member function, I get the error (where <class is the class name): <class> followed by 'int' is illegal (did you forget a ';'?)
Here's the code:
Code:
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
class number
{
private:
int value;
public:
int returnnumber();
int enternumber(int);
}
int number::returnnumber()
{
return value;
}
int number::enternumber(int enter)
{
value = enter;
}
class hexa
{
public:
int input();
int displayhex(int)
}
int hexa::input()
{
int num;
cout << "Enter a hex number: ";
cin >> hex >> num;
return num;
}
int hexa::displayhex(int disp)
{
cout << "Hex: " << hex << disp;
}
class octal
{
public:
int input();
int displayoct(int);
}
int octal::input()
{
int num;
cout << "Entre an octal number: ";
cin >> oct >> num;
return num;
}
int octal::displayoct(int disp)
{
cout << "Oct: " << oct << disp;
}
class binary
{
public:
int input();
int displaybin(int);
}
int binary::input()
{
int a=0;
int b=0;
int power;
int dec=0;
char str[100];
for (a=0;a<100;a++)
{
str[a] = '\0';
}
cin.getline(str, 100);
while (str[b] != '\0')
{
b++;
}
b -= 1;
for (a=0;a<=b;a++)
{
power = b - a;
if (str[a] == '1')
{
dec += pow(2, power);
}
}
return dec;
}
int bin::displaybin(int dec)
{
int a=0;
while (dec != 0)
{
if (dec % 2 == 1)
{
str[a] = '1';
}
else
{
str[a] = '0';
}
dec = dec / 2;
a++;
}
for (a;a>=0;a--)
{
cout << str[a];
}
}
int main(void)
{
return 0;
}
-
Whoops! There are a few other errors in this code from a cut-and-paste job. Here's the corrected version!
Code:
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
class number
{
private:
int value;
public:
int returnnumber();
int enternumber(int enter);
}
int number::returnnumber()
{
return value;
}
int number::enternumber(int enter)
{
value = enter;
}
class hexa
{
public:
int input();
int displayhex(int disp)
}
int hexa::input()
{
int num;
cout << "Enter a hex number: ";
cin >> hex >> num;
return num;
}
int hexa::displayhex(int disp)
{
cout << "Hex: " << hex << disp;
}
class octal
{
public:
int input();
int displayoct(int disp);
}
int octal::input()
{
int num;
cout << "Entre an octal number: ";
cin >> oct >> num;
return num;
}
int octal::displayoct(int disp)
{
cout << "Oct: " << oct << disp;
}
class binary
{
public:
int input();
int displaybin(int dec);
}
int binary::input()
{
int a=0;
int b=0;
int power;
int dec=0;
char str[100];
for (a=0;a<100;a++)
{
str[a] = '\0';
}
cin.getline(str, 100);
while (str[b] != '\0')
{
b++;
}
b -= 1;
for (a=0;a<=b;a++)
{
power = b - a;
if (str[a] == '1')
{
dec += pow(2, power);
}
}
return dec;
}
int binary::displaybin(int dec)
{
char str[100];
int a=0;
for (a;a<100;a++)
{
str[a] = '\0';
}
while (dec != 0)
{
if (dec % 2 == 1)
{
str[a] = '1';
}
else
{
str[a] = '0';
}
dec = dec / 2;
a++;
}
for (a;a>=0;a--)
{
cout << str[a];
}
}
int main(void)
{
return 0;
}
-
Disregard this thread! I'm an idiot! :P ...Every class needs to have a ';' at the end!
-
Adding a semicolon after the last brace of every class definition will get rid of those errors, but after doing this you'll be hit with another wall of compiler warnings or errors. You're using several undeclared variables, among a couple of smaller problems.
And while your compiler may be using the out-of-date C++, you should be aware that your code is in an obsolete style of C++. For standard headers, drop the .h extension, and precede standard C headers with a c (for example, cstdlib, cmath, and cstring if you're using C-style strings instead of STL strings). You'd also need to specify your namespace. The simplest way to do this is to simply add "using namespace std;" after you include all your headers.
-
You end a class with ;
e.g.:
Code:
class classname
{
}; <-- Here
And you forgot
using namespace std;
or
using std::cout and so on with those you use.. :)