I am having a c++ linking problem. I am trying to define a Point2D struct for all of my other files to use. Here it is.
#ifndef Point2D_h
#define Point2D_h
#include <iostream>
struct Point2D {
float x;
float y;
Point2D(float x, float y) : x(x), y(y) {}
Point2D() : x(0), y(0) {}
Point2D operator+(const Point2D& a) const {
return Point2D(a.x + x, a.y + y);
}
….
};
std::ostream& operator<<(std::ostream& os, const Point2D& m) {
return os << "(" << m.x << ", " << m.y << ")";
}
#endif /* Point2D_h */
The only problem is that I get a duplicate symbol Point2D error when I try to build. I am not sure why Point2D would be a duplicate symbol since it is header guarded
Here are all my other files, whether they are header guarded and whether they use Point2D and of course their imports:
Global.h (Header Guarded)
#include <stdio.h>
#define MAX 9000
Engine Core (Header Guarded)
#import "Global.h"
#import "Tests/EngineTests.hpp"
#import "Engine/Engine.hpp"
#import "Particle/Particle.hpp"
Tests.cpp (Uses Point2D)
#include "EngineTests.hpp"
#include <iostream>
#include "../Engine/Engine.hpp"
Tests.h
#include "Global.h"
Engine.cpp
#include "Engine.hpp"
#include <iostream>
Engine.hpp (Header guarded)
#include "Global.h"
#include "../Particle/ParticleManager.hpp"
Particle.cpp
#include "Particle.hpp"
#include <iostream>
Particle.h (Header guarded, uses Point2D)
#include "Global.h"
#include "../Math/Point.h"
ParticleManager.cpp
#include "ParticleManager.hpp"
#include <iostream>
ParticleManager.h (Header guarded)
#include <stdio.h>
#include "Global.h"
#include "Particle.hpp"
And the linker says the following
duplicate symbol __ZlsRNSt3__113basic_ostreamIcNS_11char_traitsIcEEEERK7Point2D in:
…/arm64/Particle.o
…larm64/Engine.o
duplicate symbol __ZlsRNSt3__113basic_ostreamIcNS_11char_traitsIcEEEERK7Point2D in:
…/Particle.o
…/arm64/EngineTests.o
duplicate symbol __ZlsRNSt3__113basic_ostreamIcNS_11char_traitsIcEEEERK7Point2D in:
…/arm64/Particle.o
…/ParticleManager.o
ld: 3 duplicate symbols for architecture arm64