I am a newcomer to C++. I am losing my mind over this issue (I have checked and tried other solutions from people having this very issue on Stack Overflow, yet for some reason I am unable to resolve it). I have the following files -
stdafx.h
#ifndef STDAFX_H
#define STDAFX_
#endif /* STDAFX_H */
#pragma once
#include <iostream>
#include <cstdlib>
#include <cmath>
geometry.h
#ifndef GEOMETRY_H
#define GEOMETRY_H
#endif /* GEOMETRY_H */
#pragma once
double PI = 3.1415;
class Circle {
public:
double radius;
double area() const;
double circumference() const;
};
geometry.cpp
#include "geometry.h"
double Circle::area() const {
return PI*radius*radius;
}
double Circle::circumference() const {
return 2*PI*radius;
}
main.cpp
#include "stdafx.h"
#include "geometry.h"
using namespace std;
int main() {
Circle c;
c.radius = 2.0;
cout << c.area();
cout << c.circumference();
}
The error I am getting is -
> cd '/Users/arpanganguli/NetBeansProjects/Chapter8'
/usr/bin/make -f Makefile CONF=Debug
"/Library/Developer/CommandLineTools/usr/bin/make" -f nbproject/Makefile-
Debug.mk QMAKE= SUBPROJECTS= .build-conf
"/Library/Developer/CommandLineTools/usr/bin/make" -f nbproject/Makefile-
Debug.mk dist/Debug/GNU-MacOSX/chapter8
mkdir -p dist/Debug/GNU-MacOSX
g++ -o dist/Debug/GNU-MacOSX/chapter8 build/Debug/GNU-MacOSX/geometry.o
build/Debug/GNU-MacOSX/main.o
duplicate symbol _PI in:
build/Debug/GNU-MacOSX/geometry.o
build/Debug/GNU-MacOSX/main.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see
invocation)
make[2]: *** [dist/Debug/GNU-MacOSX/chapter8] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
I don't understand what I am doing wrong? I am using Netbeans 10 on MacOS (if this information is helpful). I believe what the error is trying to tell me is that I am linking the same files twice, but if I don't like main.cpp and geometry.cpp through geometry.h, I will not be able to call the function in main.cpp. Can someone please help?