3 Replies - 136 Views - Last Post: 07 February 2012 - 07:19 AM Rate Topic: -----

Topic Sponsor:

#1 UrIkOn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 06-November 11

C++:Separating Declaration From Implementation

Posted 07 February 2012 - 07:06 AM

Why I can't run 9.4-TestCircleWithDeclaration.cpp?
[Linker error] undefined reference to `Circle::Circle()'
[Linker error] undefined reference to `Circle::Circle(double)'
[Linker error] undefined reference to `Circle::getArea()'
I am using devC++.

Circle.h
class Circle
{
      public:
             double radius;
             
             Circle();
             
             Circle(double);
             
             double getArea();
};



Circle.cpp
#include "Circle.h"

Circle::Circle()
{  radius=1;  }

Circle::Circle(double newRadius)
{  radius=newRadius;  }

double Circle::getArea()
{  return radius*radius*3.14159;  }



9.4-TestCircleWithDeclaration.cpp
#include<iostream>
#include "Circle.h"
using namespace std;

int main()
{
    Circle circle1;
    Circle circle2(5.0);
    
    cout<<"The area of the circle of radius "
      <<circle1.radius<<" is "<<circle1.getArea()<<endl;
    cout<<"The area of the circle of radius "
      <<circle2.radius<<" is "<<circle2.getArea()<<endl;
      
    circle2.radius=100;
    cout<<"The area of the circle of radius "
      <<circle2.radius<<" is "<<circle2.getArea()<<endl;
      
    system("PAUSE");
    return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: C++:Separating Declaration From Implementation

#2 jimblumberg  Icon User is online

  • member icon

Reputation: 1892
  • View blog
  • Posts: 5,681
  • Joined: 25-December 09

Re: C++:Separating Declaration From Implementation

Posted 07 February 2012 - 07:09 AM

Have you added circle.cpp to your project? Linker errors usually mean either the function does not exist or you have not added the file with the function to your project.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1812
  • View blog
  • Posts: 4,891
  • Joined: 27-December 05

Re: C++:Separating Declaration From Implementation

Posted 07 February 2012 - 07:10 AM

You probably didn't add Circle.h and Circle.cpp to your project. "Add to project" requires more than just writing and saving the code.
Was This Post Helpful? 0
  • +
  • -

#4 UrIkOn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 06-November 11

Re: C++:Separating Declaration From Implementation

Posted 07 February 2012 - 07:19 AM

Oh!Thank you!
I must open a new project and add this three file into this project!
Thank You!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1