| | |
problems with compiling
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2004
Posts: 573
Reputation:
Solved Threads: 5
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:
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
C++ Syntax (Toggle Plain Text)
11 conversion from `Circle*' to non-scalar type `Circle' requested
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
You're trying to allocate memory for a static variable. You either do this:
Or you do this:
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.
C++ Syntax (Toggle Plain Text)
Circle *circle = new Circle(radius);
C++ Syntax (Toggle Plain Text)
Circle circle(radius);
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.
All my posts may be freely redistributed under the terms of the MIT license.
•
•
Join Date: Apr 2004
Posts: 573
Reputation:
Solved Threads: 5
Well that solved that problem, but now I am getting a new problem involving the linker. It outputs this
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
C++ Syntax (Toggle Plain Text)
[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
[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
•
•
Join Date: Apr 2004
Posts: 573
Reputation:
Solved Threads: 5
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.
And i don't understand why it is giving me this error.
It says the errors are with these two lines of code.
C++ Syntax (Toggle Plain Text)
area = PI * (radius * radius);
C++ Syntax (Toggle Plain Text)
circumference = 2 * PI * radius;
Last edited by Dark_Omen; Mar 22nd, 2007 at 2:25 pm.
![]() |
Similar Threads
- need help (Java)
- Some problems while compiling unidrv with another library (C++)
- Compiling .py files... (Python)
- Compiling Problems (C++)
- Problems with compiling (Java)
- compiling problems (C++)
- No time... bitch load of problems (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: error C2375: 'my_strdup' : redefinition; different linkage
- Next Thread: circular linklist
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






