Prosiłbym o wytłumaczenie, co tutaj jest źle zrobione, tzn. dlaczego kompilator pokazuje błąd.
Build messages
||=== Build: Release in main (compiler: GNU GCC Compiler) ===|
obj\Release\physics.o:physics.cpp|| multiple definition of `PhysPoint::PhysPoint(double, double, double)'|
obj\Release\main.o:main.cpp|| first defined here|
obj\Release\physics.o:physics.cpp|| multiple definition of `PhysPoint::PhysPoint(double, double, double)'|
obj\Release\main.o:main.cpp|| first defined here|
||error: ld returned 1 exit status|
||=== Build failed: 5 error(s), 0 warning(s) (0 minute(s), 4 second(s)) ===|
Build log
-------------- Build: Release in main (compiler: GNU GCC Compiler)---------------
mingw32-g++.exe -Wall -fexceptions -O2 -c C:\Users\User\Desktop\physics.cpp -o obj\Release\physics.o
mingw32-g++.exe -o bin\Release\main.exe obj\Release\main.o obj\Release\physics.o -s -static-libgcc -static-libstdc++ -static -lpthread
obj\Release\physics.o:physics.cpp:(.text+0x0): multiple definition of `PhysPoint::PhysPoint(double, double, double)'
obj\Release\main.o:main.cpp:(.text+0x0): first defined here
obj\Release\physics.o:physics.cpp:(.text+0x0): multiple definition of `PhysPoint::PhysPoint(double, double, double)'
obj\Release\main.o:main.cpp:(.text+0x0): first defined here
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 4 second(s))
5 error(s), 0 warning(s) (0 minute(s), 4 second(s))
Trzy pliki: main.cpp physics.cpp physics.h
main.cpp
#include "physics.h"
int main()
{
Animal player(0, 0, 0, true, true);
while(true)
{
}
}
physics.cpp
#include <math.h>
#include "physics.h"
Entity::Entity(double posx, double posy, double posz, bool c, bool d) : origin(posx, posy, posz)
{
collidable = c;
dynamic = d;
velx = 0.0, vely = 0.0, velz = 0.0;
gravity = 9.80665;
paused = false;
stopped = false;
timer = GetTickCount();
}
void Entity::accelerate(float ax, float ay, float az)
{
velx += ax;
vely += ay;
velz += az;
}
void Entity::brake(float bx, float by, float bz)
{
//X AXIS BRAKING
if(velx > 0 && bx < velx)
velx -= bx;
else if(velx < 0 && bx > velx)
velx += bx;
else
velx = 0;
//Y AXIS BRAKING
if(vely > 0 && by < vely)
vely -= by;
else if(vely < 0 && by > vely)
vely += by;
else
vely = 0;
//Z AXIS BRAKING
if(velz > 0 && bz < velz)
velz -= bz;
else if(velz < 0 && bz > velz)
velz += bz;
else
velz = 0;
}
void Entity::update(void)
{
while(!stopped){
if(dynamic && !paused && GetTickCount() > timer + 61)
{
timer = GetTickCount();
accelerate(0, -gravity/16, 0); //GRAVITY ACCELERATION
brake((resistance/16)*(fabs(velx)*2), (resistance/16)*(fabs(vely)*2), (resistance/16)*(fabs(velz)*2)); //AIR RESISTANCE (SIMPLE)
origin.x += velx/16; //APPLYING POSITION
origin.y += vely/16; //-||-
origin.z += velz/16; //-||-
}}
}
void Entity::start(void)
{
stopped = false;
physicsHandler = std::thread(&Entity::update, this);
}
void Entity::pause(void)
{
paused = true;
}
void Entity::unpause(void)
{
paused = false;
}
void Entity::stop(void)
{
if(!stopped)
physicsHandler.join();
stopped = true;
}
Animal::Animal(double posx, double posy, double posz, bool c, bool d) : Entity(posx, posy, posz, c, d)
{
walkSpeed = 0.8;
runSpeed = 2.3;
acceleration = 0.2;
brakes = 2.0;
resistance = 0.065;
};
physics.h
#ifndef PHYSICS_H
#define PHYSICS_H
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <thread>
#include <vector>
class PhysPoint
{
public:
double x, y, z;
void setPos(double posx = 0, double posy = 0, double posz = 0){x = posx;y = posy;z = posz;}
PhysPoint(double = 0, double = 0, double = 0);
~PhysPoint(void){}
};
PhysPoint::PhysPoint(double posx, double posy, double posz){setPos(posx, posy, posz);}
class Entity
{
public:
bool collidable;
bool dynamic;
float velx, vely, velz;
PhysPoint origin;
//std::vector<PhysPoint> vertex;
float gravity;
float resistance;
bool paused;
bool stopped;
DWORD timer;
std::thread physicsHandler;
Entity(double = 0, double = 0, double = 0, bool = false, bool = false);
~Entity(void){}
void addVertex(double posx, double posy, double posz){}
void accelerate(float ax, float ay, float az);
void brake(float bx, float by, float bz);
void start(void);
void pause(void);
void unpause(void);
void stop(void);
private:
void update(void);
};
class Animal : public Entity
{
public:
float walkSpeed;
float runSpeed;
float acceleration;
float brakes;
Animal(double = 0, double = 0, double = 0, bool = true, bool = true);
~Animal(void){}
};
#undef WIN32_LEAN_AND_MEAN
#endif