Thread: Classes won't compile

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    20

    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;
    }

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    20
    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;
    }

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    20
    Disregard this thread! I'm an idiot! :P ...Every class needs to have a ';' at the end!

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    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.

  5. #5
    the Wizard
    Join Date
    Aug 2004
    Posts
    109
    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..
    -//Marc Poulsen -//MipZhaP

    He sat down, he programmed, he got an error...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Defining multiple classes in the same header file
    By Stonehambey in forum C++ Programming
    Replies: 2
    Last Post: 08-14-2008, 10:36 AM
  2. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  3. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM
  4. VC++6: exporting classes containing templates
    By ttt in forum Windows Programming
    Replies: 2
    Last Post: 09-15-2003, 11:38 AM
  5. Sharing a variable between classes of different .CPP files
    By divingcrab in forum C++ Programming
    Replies: 5
    Last Post: 07-07-2002, 02:57 PM