Ok, so I am trying to re learn c++ and the first thing I wanted to do was make a simple class for a circle (calculate the area and circumference of the circle). When I tried to compile it I get this error:

11   conversion from `Circle*' to non-scalar type `Circle' requested

And I don't know what that means.
Here is the code it was complaining about:

#include <iostream>

#include "Circle.h"

using namespace std;

int main(){
    double radius = 0.0;
    cout << "Enter a radius\n";
    cin  >> radius;
    Circle circle = new Circle(radius);
    cout << "The circumference of the cirle is: " << circle.calcCircumference() << ". The area of the circle is: " << circle.calcArea() << endl;
    return 0;
}

Thanks

Recommended Answers

All 5 Replies

You're trying to allocate memory for a static variable. You either do this:

Circle *circle = new Circle(radius);

Or you do this:

Circle circle(radius);

Remember that when you're using pointers as in the first example, you must delete the memory when you're done with it, and to access members you have to use '->' instead of '.'

So in this example, getting it to compile is as simple as removing that "new" thingy.

Well that solved that problem, but now I am getting a new problem involving the linker. It outputs this

[Linker error] undefined reference to `Circle::Circle(double)' 
  [Linker error] undefined reference to `Circle::calcArea()' 
  [Linker error] undefined reference to `Circle::calcCircumference()' 
  [Linker error] undefined reference to `Circle::~Circle()' 
  [Linker error] undefined reference to `Circle::~Circle()' 
  ld returned 1 exit status

Does this mean I defined my class wrong. BTW here is what I did for the class:
Circle.h

#pragma once

#include <iostream>

using namespace std;

#define PI = 3.14159265

class Circle
{
public:
    //constructors and destructors
    Circle(void);
    Circle(double);
    ~Circle(void);
    //calculation methods
    double calcArea();
    double calcCircumference();
    //get set methods
    void setRadius(double);
    double getRadius();
private:
    //data members
    double area;
    double circumference;
    double radius;
};

And here is Circle.cpp

#include "Circle.h"

Circle::Circle(void){
    radius = 0.0;
    circumference = 0.0;
    area = 0.0;
}

Circle::Circle(double r){
    radius = r;
    circumference = 0.0;
    area = 0.0;
}

Circle::~Circle(void){
}

double Circle::calcArea(){
    area = PI * (radius * radius);
    return area;
}

double Circle::calcCircumference(){
    circumference = 2 * PI * radius;
    return circumference;
}

void Circle::setRadius(double r){
    radius = r;
}

double Circle::getRadius(){
    return radius;
}

Thanks

It looks like the compiler isn't reading in Circle.cpp. Do you have it added to your project?

OK, so they were all added to the project, and the linker errors didn't appear. But now it is giving me errors with the calcCircumference and calcArea methods.
It says the errors are with these two lines of code.

area = PI * (radius * radius);
circumference = 2 * PI * radius;

And i don't understand why it is giving me this error.

#define PI = 3.14159265 This statement is wrong. Preprocessor statements are not written this way. The correct way would be: #define PI 3.142 or better yet const double PI = 3.142

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.