My code will not run no matter what I change, and I’m not sure what this error actually means. I just started this class so I have to use clion, which I am fairly new to. This is cloned from github to clion.
Tried to compile my code and I keep getting this error:
Undefined symbols for architecture arm64:
"getPosition(Particle const*)", referenced from:
(anonymous namespace)::ParticleFixture_TestGetPosition_Test::TestBody() in particle_unittest.cc.o
(anonymous namespace)::ParticleFixture_TestGetPosition_Test::TestBody() in particle_unittest.cc.o
"moveCoord3D(Coord3D*, Coord3D const*, double)", referenced from:
(anonymous namespace)::ParticleFixture_TestMoveCoord3D_Test::TestBody() in particle_unittest.cc.o
(anonymous namespace)::ParticleFixture_TestMoveCoord3D_Test::TestBody() in particle_unittest.cc.o
(anonymous namespace)::ParticleFixture_TestMoveCoord3D_Test::TestBody() in particle_unittest.cc.o
"deleteParticle(Particle const*)", referenced from:
(anonymous namespace)::ParticleFixture_TestCreateDeleteParticle_Test::TestBody() in particle_unittest.cc.o
(anonymous namespace)::ParticleFixture_TestCreateDeleteParticle_Test::TestBody() in particle_unittest.cc.o
(anonymous namespace)::ParticleFixture_TestGetPosition_Test::TestBody() in particle_unittest.cc.o
(anonymous namespace)::ParticleFixture_TestGetPosition_Test::TestBody() in particle_unittest.cc.o
(anonymous namespace)::ParticleFixture_TestMoveParticle_Test::TestBody() in particle_unittest.cc.o
(anonymous namespace)::ParticleFixture_TestSetVelocity_Test::TestBody() in particle_unittest.cc.o
"length(Coord3D const*)", referenced from:
(anonymous namespace)::ParticleFixture_TestLength_Test::TestBody() in particle_unittest.cc.o
(anonymous namespace)::ParticleFixture_TestLength_Test::TestBody() in particle_unittest.cc.o
(anonymous namespace)::ParticleFixture_TestLength_Test::TestBody() in particle_unittest.cc.o
(anonymous namespace)::ParticleFixture_TestLength_Test::TestBody() in particle_unittest.cc.o
"Particle::_num_allocations", referenced from:
Particle::getNumAllocations() in particle_unittest.cc.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
This is my code.
main.cc
int main() {
Coord3D pointP = {10, 20, 30};
Coord3D pointQ = {-20, 21, -22};
std::cout << "Address of P: " << &pointP << std::endl;
std::cout << "Address of Q: " << &pointQ << std::endl << std::endl;
Coord3D *ans = fartherFromOrigin(&pointP, &pointQ);
std::cout << "ans = " << ans << std::endl; // So which point is farther?
//task c
Coord3D pos = {0, 0, 100.0};
Coord3D vel = {1, -5, 0.2};
std::cout << "Initial position: " << pos.x << " " << pos.y << " " << pos.z << std::endl;
moveCoord3D(&pos, &vel, 2.0); // object pos gets changed
std::cout << "New position: " << pos.x << " " << pos.y << " " << pos.z << std::endl;
// prints: 2 -10 100.4
// task D
double x, y, z;
std::cout << "Enter position: ";
std::cin >> x >> y >> z;
Coord3D *ppos = createCoord3D(x, y, z);
std::cout << "Enter velocity: ";
std::cin >> x >> y >> z;
Coord3D *pvel = createCoord3D(x, y, z);
moveCoord3D(ppos, pvel, 10.0);
std::cout << "Coordinates after 10 seconds: "
<< (*ppos).x << " " << (*ppos).y << " " << (*ppos).z << std::endl;
deleteCoord3D(ppos); // release memory
deleteCoord3D(pvel);
//task E
Particle *p = createParticle(1.0, 1.5, 2.0, 0.1, 0.2, 0.3);
double time = 0.0;
double dt = 0.1;
while(time < 3.0) {
setVelocity(p, 10.0 * time, 0.3, 0.1);
moveParticle(p, dt);
time += dt;
std::cout << "Time: " << time << " t";
std::cout << "Position: "
<< getPosition(p).x << ", "
<< getPosition(p).y << ", "
<< getPosition(p).z << std::endl;
}
deleteParticle(p);
return EXIT_SUCCESS;
}
Particle.cc
#include <iostream>
#include <cmath>
class Coord3D {
public:
double x;
double y;
double z;
};
double length(Coord3D *p) {
// Dereference the pointer to access x, y, z coordinates
double x = p->x;
double y = p->y;
double z = p->z;
// Compute the distance using the Euclidean distance formula
double distance = sqrt(x * x + y * y + z * z);
return distance;
}
Coord3D *fartherFromOrigin(Coord3D *p1, Coord3D *p2) {
if (length(p1) > length(p2)) {
return p1;
} else {
return p2;
}
}
Coord3D* createCoord3D(double x, double y, double z) {
Coord3D *p = new Coord3D;
p->x = x;
p->y = y;
p->z = z;
return p;
}
void deleteCoord3D(Coord3D *p) {
delete p;
}
void moveCoord3D(Coord3D *ppos, Coord3D *pvel, double dt) {
ppos->x += pvel->x * dt;
ppos->y += pvel->y * dt;
ppos->z += pvel->z * dt;
}
struct Particle {
Coord3D pos;
Coord3D vel;
};
Particle* createParticle(double x, double y, double z, double vx, double vy, double vz) {
Particle *p = new Particle;
p->pos.x = x;
p->pos.y = y;
p->pos.z = z;
p->vel.x = vx;
p->vel.y = vy;
p->vel.z = vz;
return p;
}
void setVelocity(Particle *p, double vx, double vy, double vz) {
p->vel.x = vx;
p->vel.y = vy;
p->vel.z = vz;
}
Coord3D getPosition(Particle *p);
Coord3D getPosition(Particle *p) {
return p->pos;
}
void moveParticle(Particle *p, double dt) {
p->pos.x += p->vel.x * dt;
p->pos.y += p->vel.y * dt;
p->pos.z += p->vel.z * dt;
}
void deleteParticle(Particle *p) {
delete p;
}
Particle.h
#ifndef PARTICLE_H
#define PARTICLE_H
#include <cstdlib>
struct Coord3D {
double x;
double y;
double z;
};
// get the distance from the given coordinate to the origin
double length(const Coord3D* p);
// return the coordinate that's farthest from the origin
Coord3D* fartherFromOrigin(Coord3D* p1, Coord3D* p2);
// advance the position of the coordinate traveling at a given speed for a
// given time
void moveCoord3D(Coord3D* ppos, const Coord3D* pvel, double dt);
struct Particle {
public:
double x;
double y;
double z;
double vx;
double vy;
double vz;
static size_t getNumAllocations() { return _num_allocations; }
void* operator new(const size_t t) {
(void)t;
_num_allocations++;
return ::new Particle();
}
void operator delete(void *const p) {
_num_allocations--;
free(p);
}
private:
static size_t _num_allocations;
};
// dynamically allocate memory for a particle and initialize it
Particle* createParticle(double x, double y, double z,
double vx, double vy, double vz);
// set its velocity to (vx, vy, vz)
void setVelocity(Particle* p, double vx, double vy, double vz);
// get its current position
Coord3D getPosition(const Particle* p);
// update particle's position after elapsed time dt
void moveParticle(Particle* p, double dt);
// delete all memory allocated for the particle passed by pointer
void deleteParticle(const Particle* p);
#endif // PARTICLE_H
New contributor
Lucy Shah is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.