Dec 4, 2018 at 3:06pm UTC
I am trying to make a custom class and for whatever reason it wont work, i'd be very grateful if someone were to help and give me an explanation of what im doing wrong.
Circle.cpp code:
_________________________________________________________________________
#include <string>
#ifndef _CIRCLE
#define _CIRCLE_h
using namespace std;
const double PI = 3.14159;
class circle
{
public:
circle();
circle(const circle &);
void SetRadius(double);
double Area();
private:
double radius;
};
circle::circle()
{
radius = 0.0;
}
circle::circle(const circle & Object)
{
radius = Object.radius;
}
void circle::SetRadius(double IncomingRadius)
{
radius = IncomingRadius;
}
double circle::Area()
{
return(PI*radius*radius);
}
#endif
Errors: None
_________________________________________________________________________
circle.h code:
_________________________________________________________________________
#ifndef _CIRCLE
#define _CIRCLE_h
#include <string>
using namespace std;
double PI = 3.14159;
class circle
{
public:
circle();
circle(const circle &);
void SetRadius(double);
double Area();
private:
double radius;
};
circle::circle()
{
radius = 0.0;
}
circle::circle(const circle & Object)
{
radius = Object.radius;
}
void circle::SetRadius(double IncomingRadius)
{
radius = IncomingRadius;
}
double circle::Area()
{
return(PI*radius*radius);
}
#endif
Errors: None
_________________________________________________________________________
Source.cpp code:
_________________________________________________________________________
#include <iostream>
#include "circle.h"
using namespace std;
int main()
{
circle Circle_One;
circle Circle_Two;
double User_Radius;
double area;
cout << "\nWhat is the radius of the first circle? ";
cin >> User_Radius;
Circle_One.SetRadius(User_Radius);
cout << "\nWhat is the radius of the second circle?";
cin >> User_Radius;
Circle_Two.SetRadius(User_Radius);
area = Circle_One.Area();
cout.setf(ios::fixed);
cout << "\nThe area of the first circle is " << area << ".\n";
area = Circle_Two.Area();
cout << "\nThe area of the second choice is " << area << ".\n";
cin.get();
while (1);
return 0;
}
Errors:
_________________________________________________________________________
Errors:
Severity Code Description Project File Line Suppression State
Error LNK2005 "public: __thiscall circle::circle(class circle const &)" (??0circle@@QAE@ABV0@@Z) already defined in Circle.obj CircleTester H:\11th Grade\ADV C++\CircleTester\CircleTester\Source.obj 1
Error LNK2005 "public: __thiscall circle::circle(void)" (??0circle@@QAE@XZ) already defined in Circle.obj CircleTester H:\11th Grade\ADV C++\CircleTester\CircleTester\Source.obj 1
Error LNK2005 "public: double __thiscall circle::Area(void)" (?Area@circle@@QAENXZ) already defined in Circle.obj CircleTester H:\11th Grade\ADV C++\CircleTester\CircleTester\Source.obj 1
Error LNK2005 "public: void __thiscall circle::SetRadius(double)" (?SetRadius@circle@@QAEXN@Z) already defined in Circle.obj CircleTester H:\11th Grade\ADV C++\CircleTester\CircleTester\Source.obj 1
Error LNK1169 one or more multiply defined symbols found CircleTester H:\11th Grade\ADV C++\CircleTester\Debug\CircleTester.exe 1
Dec 4, 2018 at 3:35pm UTC
One of your issues is that you are improperly making your
header guards .
Two rules:
1. Only put header guards in the Header file (.h), not Implementation file (.cpp).
Solution: Remove the #ifndef/#define/#endif trio from your circle.cpp file.
2. It should follow this pattern in the header file:
1 2 3 4 5 6
#ifndef MY_UNIQUE_HEADER_NAME
#define MY_UNIQUE_HEADER_NAME
...
#endif
Notice: both the #ifndef and the #define line have the
same name next to them.
You have this:
1 2 3 4
#ifndef _CIRCLE
#define _CIRCLE_h
...
#endif
This won't work, because you never define _CIRCLE.
PS: Don't make macros starting with underscores followed by a capital letter. They're technically forbidden by the standard.
Edit: coder777's post is actually more directly your issue than my post, but header guards are still important, especially if you #include it in more than one place.
Last edited on Dec 4, 2018 at 4:18pm UTC
Dec 4, 2018 at 3:41pm UTC
You have the same code in Circle.cpp and circle.h? This does not make sense.
Remove the class definition from the .cpp and the implementation from the .h file.