Thread: Constructor and destructor

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    81

    Unhappy Constructor and destructor

    Hi

    I cannot compile the following code:

    Code:
    #include <iostream.h>
    
    int count = 0;
    
    class point{
    	int x, y;
    
    public:
    	point(int a=0, int b=0)
    	{
    		x=a;
    		y=b;
    		count++;
    		cout << "Point created: x=" << x << " y=" << y << endl;
    	}
    
    	void set(int a, int b)
    	{
    		x=a;
    		y=b;
    	}
    
    	void get(int &a, int &b)
    	{
    		a=x;
    		b=y;
    	}
    
    	void move(int a, int b)
    	{
    		x=x+a;
    		y=y+b;
    	}
    
    	~point()
    	{
    		count = count-1;
    		cout << " Point destroyed: x=" << x << " y=" << y << endl;
    	}
    }
    
    int quantity(){
    	return count;
    }
    
    int main()
    {
    	cout << "Name of points:" << quantity() << endl;
    	point p1, p2(30), p3(50,70);
    	cout << "Number of points: " << quantity() << endl;
    	return 0;
    }
    error C2628: 'point' followed by 'int' is illegal (did you forget a ';'?)

    error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class point' (or there is no acceptable conversion)

    error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class point' (or there is no acceptable conversion)

    What is wrong with my code?

    Thanks



  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    error C2628: 'point' followed by 'int' is illegal (did you forget a ';'?)
    Did you forget a semicolon? YES YOU DID!
    Right after your class declaration...
    Code:
    class point
    {
       // ...
    }; // <-- Semicolon needed
    As for your other errors, it's 3AM and I don't know. Try adding that semicolon, it might clear up all three errors.

  3. #3
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Just a design improvement. Why not encapsulate a count data-memeber insteed of having it outside the class? It makes more sense...

    Code:
    #include <iostream.h>
    #include <conio.h>
    
    class point{
    	int x, y;
    	static int count;
    
    public:
    	point(int a=0, int b=0)
    	{
    		x=a;
    		y=b;
    		count++;
    		cout << "Point created: x=" << x << " y=" << y << endl;
    	}
    
    	void set(int a, int b)
    	{
    		x=a;
    		y=b;
    	}
    
    	void get(int &a, int &b)
    	{
    		a=x;
    		b=y;
    	}
    
    	void move(int a, int b)
    	{
    		x=x+a;
    		y=y+b;
    	}
    
    	~point()
    	{
    		count--;
    		cout << " Point destroyed: x=" << x << " y=" << y << endl;
    	}
    	
    	static int quantity()
    	{
    	    return count;
    	}
    };
    
    int point::count = 0;
    
    
    
    int main()
    {
    	cout << "Number of points:" << point::quantity() << endl;
    	point p1, p2(30), p3(50,70);
    	cout << "Number of points: " << point::quantity() << endl;
    	getch();
    	return 0;
    }

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    81
    Thanks for teaching me the encapsulation.

    May I ask you whether all classes have to be ended with ';'?

    and, what is the header file: #include <conio.h> ?

    Thanks for help

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    yes, classes need to be ended with a semicolon. along with structs, and enum data types.

    conio.h was used for getch(), which is used to pause the program. you don't really need to worry about it, it is non-standard.

Popular pages Recent additions subscribe to a feed