Thread: Inheritance:

  1. #1
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499

    Inheritance:

    I am getting runtime errors that say:

    Code:
    Linker (id) Error
    "circle::circle()" reference from:
    
    Linker (id) Error
    "cylinder::cylinder()",reference from:
    Code:
    #include <iostream>
    
    using namespace std;
    
    class circle {
        
    protected:
        double radius=1.0;
    
    public:
        circle();
        
        const double PI = 3.14;
        
        void setRadius(double a){
            radius=a;
        }
        
        double calcVal(){
            
            return PI*radius*radius;
        }
    };
    
    class cylinder : public circle {
    protected:
        int length;
        
    public:
        cylinder();
        
        double calcVal()
        {
            return length * circle::calcVal();
            
        }
        
        
    };
    
    int main()
    {
        double radius;
        
        cylinder v;
        circle c;
        
        cout<<"Enter the radius of the circle in question: "<<endl;
        cin>>radius;
        
        c.setRadius(radius);
       
        cout<<"The area of the circle is: "<<c.calcVal()<<endl;
        cout<<"The volume of the circle is:"<<v.calcVal()<<endl;
        
        
        
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You forgot to define the default constructor for circle and cylinder.

    Note that the errors are not runtime errors. They are linker/link time errors. When you get such errors that mention "undefined reference" or something along those lines, look at the error message and see if you defined the function (or sometimes, check if you included the file containing the function definition in your project/build script).
    Last edited by laserlight; 11-09-2013 at 01:45 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499


    This is the second time you had to remind me about this....

    I been programming for 9 hours today. I had to make a chess game and a address book with a bunch of switch statements. I think I should go lay down...

    Thank you laser light.....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GDB and inheritance
    By markcole in forum C++ Programming
    Replies: 10
    Last Post: 02-04-2009, 09:19 AM
  2. Inheritance
    By Verdagon in forum C++ Programming
    Replies: 8
    Last Post: 06-12-2005, 10:58 AM
  3. Inheritance...
    By EvBladeRunnervE in forum C++ Programming
    Replies: 3
    Last Post: 08-26-2003, 01:13 PM
  4. Inheritance?
    By Krak in forum C++ Programming
    Replies: 3
    Last Post: 07-16-2003, 08:32 PM
  5. inheritance help...
    By tetra in forum C++ Programming
    Replies: 5
    Last Post: 05-12-2003, 07:56 PM