Trouble with constructors and destructors in classes

This is a discussion on Trouble with constructors and destructors in classes within the C++ Programming forums, part of the General Programming Boards category; Code: Im working on a a circle class program in which I have to find the diameter and area of ...

  1. #1
    Registered User ralocorvette's Avatar
    Join Date
    Nov 2010
    Posts
    8

    Trouble with constructors and destructors in classes

    Code:
    Im working on a a circle class program in which I have to find the diameter and area of the circle. I have divided the program into 3 files, 1 main.cpp 1 circle.cpp and 1 circle.h
    My code produces and error:
    
    1>main_Circle.obj : error LNK2019: unresolved external symbol "public: __thiscall Circle::~Circle(void)" (??1Circle@@QAE@XZ) referenced in function _main
    1>main_Circle.obj : error LNK2019: unresolved external symbol "public: __thiscall Circle::Circle(double)" (??0Circle@@QAE@N@Z) referenced in function _main
    1>C:\Users\RKLO\documents\visual studio 2010\Projects\Rectangle class2\Debug\Rectangle class2.exe : fatal error LNK1120: 2 unresolved externals
    
    
    Here is my code
    
    
    //Cirlcle.h
    #pragma once
    //Circle class declaration
    
    class Circle
    {
    	private:
    		double radius;
    	
    	public:
    
    	Circle(double=0);
    	Circle (const Circle &);
    	~Circle();
    	
    	void setRadius(double);
    	double getRadius()const;
    	double getDiameter()const;
    	double getArea()const;
    };
    
    //Circle.cpp
    
    //Implementation file for Circle class
    
    #include "Circle.h"//Needed for the rectangle class
    #include<iostream>//Needed for the cout
    #include<cstdlib>//Needed for the exit function
    using namespace std;
    
    //sets the value of the member radius
    
    
    void Circle::setRadius(double r)
    {
    	if (r>=0) {
    		radius=r;
    	}
    	else {
    		cout << "Invalid radius\n";
    		exit(EXIT_FAILURE);
    	}
    }
    
    //returns the value mamber of the variable radius
    double Circle::getRadius()const
    {
    	return radius;
    }
    //returns the value member of the variable Diameter
    double Circle:: getDiameter()const
    {
    	return 2*radius;
    }
    //get area returns the pruduct times pi*radius^2
    
    double Circle:: getArea()const
    {
    	return 3.1416*radius*radius;
    }
    
    //main_Circle.cpp
    
    #include <iostream>
    #include "Circle.h"//Neede for the rectangle class
    using namespace std;
    
    int main()
    {
    	Circle box;
    	double radiusCircle;//Local variable for radius
    	cout<<"This program will calculate the area of a\n" ;
    	cout << "circle. What is the radius? ";
    	cin>>radiusCircle;
    	
    	box.setRadius(radiusCircle);
    	
    	cout << "Here is the circle's data: ";
    	cout << "Radius: "<<box.getRadius()<<endl;
    	cout << "Diameter "<<box.getDiameter()<<endl;
    	cout << "The Area of the Circle is "<<box.getArea()<<endl;
    	
    	return 0;
    }
    
    
    Any help will be really appreciated
    I have uploaded the three files for your convenience.
    Attached Files Attached Files

  2. #2
    a guy with long hair Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    103
    You didn't implement the constructor and destructor.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    Don't put your whole post in code tags, only the part which contains actual code.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    13
    To re-ellaborate on what Xupicor is saying... You didn't write a default constructor.
    Code:
    Circle() { };
    Once you write a constructor C++ stops making a default for you. Since your code in main calls
    Code:
    Circle box;  //Doesn't exist no Circle() function in class Circle (You wrote Circle(double=0))
    you get an error. You have to write Circle(double=0) as Circle() to get your code to work. Plus you never defined Circle(const Circle &) or Circle(double=0) anyways so yeah, they don't work if they have no definition :P

    Simply put, change Circle(double=0) to Circle() { } and define Circle(const Circle &) in the Circle.cpp file....

  5. #5
    Registered User ralocorvette's Avatar
    Join Date
    Nov 2010
    Posts
    8
    Thanks guys I fixed my program it worked really nice.

  6. #6
    Registered User ralocorvette's Avatar
    Join Date
    Nov 2010
    Posts
    8

    Circle Class (working version)

    Code:
    Here is my fixed code for the benefit of anyone:
    
    //Circle.h
    
    #pragma once
    const double pi=3.1416;
    class Circle 
    {
    private:
    	double diameter,area,radius;
    public:
    
    	Circle(double=0.0);
    	Circle(const Circle&);
    	~Circle();
    	void SetDiameter(double);
    	void setArea(double);
    	void setRadius(double a);
    	double getDiameter()const;
    	double getRadius()const;
    	double getArea()const;
    	void printData()const;
    
    };//end class
    
    //Circle.cpp
    #include "Circle.h" //needed for the class
    #include<iostream>//needed for the cout
    #include<cstdlib>
    using namespace std;
    
    void Circle::setRadius(double r)
    {
    if(r>0){
    	radius=r;
    }
    else{
    	cout<<"Invalid Radius\n";
    	exit(EXIT_FAILURE);
    }
    
    }//end set radio
    void Circle::setArea(double a)
    {
    	area =a;
    }//end set area
    
    void Circle::SetDiameter(double d)
    {
    	diameter=d;
    }//end set diameter
    
    double Circle::getRadius()const
    {
    	return radius;
    }//end getRadius
    
    double Circle::getArea()const
    {
    	return pi*radius*radius;
    }//end getArea
    
    double Circle::getDiameter()const
    {
    	return 2*radius;
    }//end getDiameter
    
    void Circle::printData()const
    {
    	cout<<"Radius: "<<getRadius()<<endl;
    	cout<<"Diameter: "<<getDiameter()<<endl;
    	cout<<"Area: "<<getArea()<<endl;
    	cout<<endl;
    }//end printData
    
    Circle::Circle(double)
    {}//end constructor
    
    Circle::Circle(const Circle & instance)
    {}//end constructor
    
    Circle::~Circle()
    {}//end destructor
    
    //main_circle.cpp
    
    #include<iostream>
    #include<cstdio>
    #include<cctype>
    using namespace std;
    #include "circle.h"
    
    int main()
    {
    	Circle box1,box2;
    	char e;
    	double r,CirArea,CirDiam;
    		
    	do{
    	cout<<"This program will calculate the area of a \n";
    	cout<<"Circle; What is the radius?"<<endl;
    	cin>>r;
    	box1.setRadius(r);
    	box2.setRadius(r);
    	box2.printData();
    	cout<<endl;
    	
    cout<<" The Area of the Cirlce is: "<< box1.getArea() <<endl<<endl;
    cout<<"Type any non-digit character to exit"<<endl;
    cout<<"Press any number to continue and then hit Enter"<<endl;
    cin>>e;
    	
    	//box1.getArea();
    	//box1.getDiameter();
    
    
    	cout<<"This program will calculate the area of a \n";
    	cout<<"Circle; What is the radius?"<<endl;
    	cin>>r;
    
    	box2.setRadius(r);
    	box2.printData();
    	cout<<endl;
    
    cout<<"Type a non-numeric value to exit"<<endl;
    cout<<"Press any number to continue and then hit Enter"<<endl;
    	cin>>e;	}while(isdigit(e));
    
    	system("pause");
    	return 0;
    }//end main
    
    I uploaded the files for the benefit of the user.
    Attached Files Attached Files

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    <sigh>
    Attached Images Attached Images   
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    a guy with long hair Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    103
    So you fixed it, great. But...
    Quote Originally Posted by hk_mp5kpdw View Post
    Don't put your whole post in code tags, only the part which contains actual code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about Constructors and Destructors with Pointers
    By specter in forum C++ Programming
    Replies: 14
    Last Post: 04-01-2010, 09:39 AM
  2. Constructors and Destructors
    By lasher in forum C++ Programming
    Replies: 2
    Last Post: 12-03-2006, 10:53 PM
  3. Destructors and Constructors on classes
    By Da-Nuka in forum C++ Programming
    Replies: 14
    Last Post: 06-14-2005, 02:08 PM
  4. Constructors and Destructors
    By GravtyKlz in forum C++ Programming
    Replies: 7
    Last Post: 03-09-2003, 09:44 AM
  5. Classes: constructors, destructors ???
    By mbeisser21 in forum C++ Programming
    Replies: 18
    Last Post: 07-21-2002, 09:33 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21