Hey guys. Ive been trying to write this program for a while and I keep getting errors on line 78, "Circle undeclared." I think it has something to do with the constructors and templates. I dont think im calling it right. Can someone take a look at it for me and tell me where I went wrong? I think I have everything else right(hopefully). I think if you read the code you can see what the program is supposed to do. Thanks!

#include <iostream>
#include <cstdlib>
#include <cmath>
#define pi 3.14159265
using namespace std;
template <class T>
class circle {
  public:
    circle();
    circle(T,T,T,T);
    void populate_classobj(T a1, T b1, T a2, T b2);
    T radius();
    T circumference();
    T area();
  private:
    T x1,x2,y1,y2;
  protected:
    T distance();
}; //End of class
//Function definitions
template <class T>
circle<T>::circle(T a1,T b1,T a2,T b2)
{
  x1=a1;
  y1=b1;
  x2=a2;
  y2=b2;
}
template <class T>
circle<T>::circle()
{
  x1=0;
  x2=0;
  y1=0;
  y2=0;
}
template <class T>
void circle<T>::populate_classobj(T a1, T b1, T a2, T b2)
{
  x1=a1;
  y1=b1;
  x2=a2;
  y2=b2;
}
template <class T>
T circle<T>::radius()
{
  return distance();
}
template <class T>
T circle<T>::circumference()
{
  return pi*radius()*2;
}
template <class T>
T circle<T>::area()
{
  return pi*pow(radius(),2);
}
template <class T>
T circle<T>::distance()
{
  return sqrt( pow((x2-x1),2) + pow((y2-y1),2) );
}
 
int main()    //Main
{
  //Initialize variables
  float x1,x2,y1,y2;
  int input=0;
  //Declare object & variables
  circle my_obj1 (1,3);
  circle my_obj2 (1.5,-0.5,-6.65,10.0);
  while(input<4)
  {
    //Choose functions to compute
    cout<<"Press 1 to compute the radius of the circle"<<endl;
    cout<<"Press 2 to compute the circumference"<<endl;
    cout<<"Press 3 to compute the area of the circle"<<endl;
    cout<<"Press 4 to enter new values for Object 1"<<endl;
    cout<<"Press 5 to enter new values for Object 2"<<endl;
    cout<<"Press 6 to exit"<<endl;
    cin>>input;
    //If statements
    if(input>6)
    {
      cout<<"PLEASE PICK A NUMBER BETWEEN 1 AND 6"<<endl;;
      input=0;
    }
    if(input==1)
    {
      cout<<"Radius of object 1 equals: "<< my_obj1.radius() <<endl;
      cout<<"Radius of object 2 equals: "<< my_obj2.radius() <<endl;
    }
    if(input==2)
    {
      cout<<"Circumference of object 1 equals: "<< my_obj1.circumference() <<endl;
      cout<<"Circumference of object 2 equals: "<< my_obj2.circumference() <<endl;
    }
    if(input==3)
    {
      cout<<"Area of object 1 equals: "<< my_obj1.area() <<endl;
      cout<<"Area of object 2 equals: "<< my_obj2.area() <<endl;
    }
    if(input==4)
    {
      cout<<"Please enter new values for object 1: "<<endl;
      my_obj1.populate_classobj(x1,x2,y1,y2);
    }
    if(input==5)
    {
      cout<<"Please enter new values for object 2: "<<endl;
      my_obj2.populate_classobj(x1,x2,y1,y2);
    }
    if(input==6)
    {
      return EXIT_SUCCESS;
    }
  }//While loop end
  //Exit program
  system("PAUSE");
  return 0;
}
#include <iostream>
#include <cstdlib>
#include <cmath>
#define pi 3.14159265
using namespace std;
template <class T>
class circle {
  public:
    circle();
    circle(T,T,T,T);
    void populate_classobj(T a1, T b1, T a2, T b2);
    T radius();
    T circumference();
    T area();
  private:
    T x1,x2,y1,y2;
  protected:
    T distance();
}; //End of class
//Function definitions
template <class T>
circle<T>::circle(T a1,T b1,T a2,T b2)
{
  x1=a1;
  y1=b1;
  x2=a2;
  y2=b2;
}
template <class T>
circle<T>::circle()
{
  x1=0;
  x2=0;
  y1=0;
  y2=0;
}
template <class T>
void circle<T>::populate_classobj(T a1, T b1, T a2, T b2)
{
  x1=a1;
  y1=b1;
  x2=a2;
  y2=b2;
}
template <class T>
T circle<T>::radius()
{
  return distance();
}
template <class T>
T circle<T>::circumference()
{
  return pi*radius()*2;
}
template <class T>
T circle<T>::area()
{
  return pi*pow(radius(),2);
}
template <class T>
T circle<T>::distance()
{
  return sqrt( pow((x2-x1),2) + pow((y2-y1),2) );
}
 
int main()    //Main
{
  //Initialize variables
  float x1,x2,y1,y2;
  int input=0;

// specyin type is must

   circle<double> my_obj1 (1,3, 4, 5); 

// you didnt write a constructor which took 2 args but still were 
// constructing objs by passing in just 2 values 

   circle<double> my_obj2 (1.5,-0.5,-6.65,10.0);


  while(input<4)
  {
    //Choose functions to compute
    cout<<"Press 1 to compute the radius of the circle"<<endl;
    cout<<"Press 2 to compute the circumference"<<endl;
    cout<<"Press 3 to compute the area of the circle"<<endl;
    cout<<"Press 4 to enter new values for Object 1"<<endl;
    cout<<"Press 5 to enter new values for Object 2"<<endl;
    cout<<"Press 6 to exit"<<endl;
    cin>>input;
    //If statements
    if(input>6)
    {
      cout<<"PLEASE PICK A NUMBER BETWEEN 1 AND 6"<<endl;;
      input=0;
    }
    if(input==1)
    {
      cout<<"Radius of object 1 equals: "<< my_obj1.radius() <<endl;
      cout<<"Radius of object 2 equals: "<< my_obj2.radius() <<endl;
    }
    if(input==2)
    {
      cout<<"Circumference of object 1 equals: "<< my_obj1.circumference() <<endl;
      cout<<"Circumference of object 2 equals: "<< my_obj2.circumference() <<endl;
    }
    if(input==3)
    {
      cout<<"Area of object 1 equals: "<< my_obj1.area() <<endl;
      cout<<"Area of object 2 equals: "<< my_obj2.area() <<endl;
    }
    if(input==4)
    {
      cout<<"Please enter new values for object 1: "<<endl;
      my_obj1.populate_classobj(x1,x2,y1,y2);
    }
    if(input==5)
    {
      cout<<"Please enter new values for object 2: "<<endl;
      my_obj2.populate_classobj(x1,x2,y1,y2);
    }
    if(input==6)
    {
      return EXIT_SUCCESS;
    }
  }//While loop end
  //Exit program
  system("PAUSE");
  return 0;
}

When using template using when creatign objects you need to specify the type along with the class. FOr eg. if circle is a template class then while creating objects i need to specify the type which will be taken in plave of the "T".

I have highlighted the problem ares in your code in red. Just make the changes and it will be fine.

Thanks for the response. But one question, the program is supposed to not be constricted to a certain type of numbers. It is supposed to accept all types ie float, int, double, etc. Is there a way to do something like the following?

circle<T> my_obj1 (1,3);

Thanks again for the response and the help

Write a costructor which accepts 4 arguments along with the constructor which you have written which accpets 2 args.

Write something like

Circle (T a, T b, T c, T d) { ... }

Bye.

Hmm looks like i did some typing mistake in my previous post. But just to make my point concrete here is a small snippet:

circle<T>::circle(T a,T b)
{
  x1 = a;
  y1 = b; // you can keep even "a" here depends on ur requirement
  x2 = a;
  y2 = b;
}

Just write this additional constructor and everything should work out fine for you. And if your problem is solved post again so that others can gain help from it and the MOD can mark it as solved.

Hey thanks for all the help but I figured out the syntax. When I declare the constructor i have to declare it like this:

circle(T=0,T=0,T=0,T=0);

So it will set the initial values of the numbers put into their to 0. Then in the constructor definiton it should be this:

circle<T>::circle(T a1,T b1,T a2,T b2)
{
  x1=a1;
  y1=b1;
  x2=a2;
  y2=b2;
}

So if values are not put into the object when called in main() they will be initialized to 0.
So calls like, circle<int> myobj_1(1,2) are legal.

This question has already been answered. Start a new discussion instead.