943,568 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1735
  • C++ RSS
Mar 21st, 2007
0

problems with compiling

Expand Post »
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:
C++ Syntax (Toggle Plain Text)
  1. 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:
[PHP]
#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;
}
[/PHP]

Thanks
Similar Threads
Reputation Points: 23
Solved Threads: 6
Posting Pro
Dark_Omen is offline Offline
573 posts
since Apr 2004
Mar 21st, 2007
0

Re: problems with compiling

You're trying to allocate memory for a static variable. You either do this:
C++ Syntax (Toggle Plain Text)
  1. Circle *circle = new Circle(radius);
Or you do this:
C++ Syntax (Toggle Plain Text)
  1. 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.
Last edited by John A; Mar 21st, 2007 at 8:36 pm.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Mar 21st, 2007
0

Re: problems with compiling

Well that solved that problem, but now I am getting a new problem involving the linker. It outputs this
C++ Syntax (Toggle Plain Text)
  1. [Linker error] undefined reference to `Circle::Circle(double)'
  2. [Linker error] undefined reference to `Circle::calcArea()'
  3. [Linker error] undefined reference to `Circle::calcCircumference()'
  4. [Linker error] undefined reference to `Circle::~Circle()'
  5. [Linker error] undefined reference to `Circle::~Circle()'
  6. ld returned 1 exit status
  7.  

Does this mean I defined my class wrong. BTW here is what I did for the class:
Circle.h
[PHP]
#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;
};
[/PHP]

And here is Circle.cpp
[PHP]
#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;
}
[/PHP]

Thanks
Reputation Points: 23
Solved Threads: 6
Posting Pro
Dark_Omen is offline Offline
573 posts
since Apr 2004
Mar 21st, 2007
0

Re: problems with compiling

It looks like the compiler isn't reading in Circle.cpp. Do you have it added to your project?
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Mar 22nd, 2007
0

Re: problems with compiling

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.
C++ Syntax (Toggle Plain Text)
  1. area = PI * (radius * radius);
C++ Syntax (Toggle Plain Text)
  1. circumference = 2 * PI * radius;
And i don't understand why it is giving me this error.
Last edited by Dark_Omen; Mar 22nd, 2007 at 2:25 pm.
Reputation Points: 23
Solved Threads: 6
Posting Pro
Dark_Omen is offline Offline
573 posts
since Apr 2004
Mar 22nd, 2007
0

Re: problems with compiling

#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
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: error C2375: 'my_strdup' : redefinition; different linkage
Next Thread in C++ Forum Timeline: circular linklist





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC