problems with compiling

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2004
Posts: 573
Reputation: Dark_Omen is an unknown quantity at this point 
Solved Threads: 5
Dark_Omen Dark_Omen is offline Offline
Posting Pro

problems with compiling

 
0
  #1
Mar 21st, 2007
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:
  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
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: problems with compiling

 
0
  #2
Mar 21st, 2007
You're trying to allocate memory for a static variable. You either do this:
  1. Circle *circle = new Circle(radius);
Or you do this:
  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.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 573
Reputation: Dark_Omen is an unknown quantity at this point 
Solved Threads: 5
Dark_Omen Dark_Omen is offline Offline
Posting Pro

Re: problems with compiling

 
0
  #3
Mar 21st, 2007
Well that solved that problem, but now I am getting a new problem involving the linker. It outputs this
  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
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: problems with compiling

 
0
  #4
Mar 21st, 2007
It looks like the compiler isn't reading in Circle.cpp. Do you have it added to your project?
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 573
Reputation: Dark_Omen is an unknown quantity at this point 
Solved Threads: 5
Dark_Omen Dark_Omen is offline Offline
Posting Pro

Re: problems with compiling

 
0
  #5
Mar 22nd, 2007
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.
  1. area = PI * (radius * radius);
  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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,619
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: problems with compiling

 
0
  #6
Mar 22nd, 2007
#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
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC