So I'm new to C++, I was mainly reading out of a book, but after a while I realized it was pretty vague. Currently just running through some of my knowledge with some examples,but I'm currently pulling out my hair with this bug, I've tried retracing my steps and starting from scratch twice, and end up with the same issue.
Problem: I have a vehicle class, that has an Engine
and a Tank
, but when compiling I get an error of undefined symbols.
Xcode error:
Undefined symbols for architecture x86_64: "Tank::Tank(float)", referenced from:
Vehicle::Vehicle(int, float, int) in Vehicle.o "Tank::~Tank()", referenced from:
Vehicle::Vehicle(int, float, int) in Vehicle.o "Engine::Engine(int)", referenced from:
Vehicle::Vehicle(int, float, int) in Vehicle.o "Engine::~Engine()", referenced from:
Vehicle::Vehicle(int, float, int) in Vehicle.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
Given Example: Building a car
Folder Structure:
Tank/
Tank.h
Tank.cpp
Engine/
Engine.h
Engine.cpp
Vehicle.h
vehicle.cpp
main.cpp
Tank.h
:
#ifndef __Test__Tank__
#define __Test__Tank__
class Tank
{
public:
Tank (float);
virtual ~Tank();
protected:
const float tankCapacity;
};
#endif
Tank.cpp
:
#include <iostream>
#include "Tank.h"
using namespace std;
//Implementation of constructor
Tank::Tank(float capacity) : tankCapacity(capacity){
cout << "Tank was created...with a tank capacity of " << this->getTankCapacity()<< endl;
}
//Implementation of destructor
Tank::~Tank(){
cout << "Tank was destroyed..." << endl;
}
Engine.h
:
#ifndef __Test__Engine__
#define __Test__Engine__
#include <iostream>
#include "../Tank/Tank.h"
class Engine
{
public:
Engine(int);
~Engine();
protected:
bool started;
int trip, numCyl;
};
#endif /* defined(__Test__Engine__) */
Engine.cpp
:
#include <iostream>
#include "Engine.h"
using namespace std;
Engine::Engine(int num) : numCyl(num)
{
this->started = false;
this->trip = 0;
cout << "Engine was created with " << this->numCyl << " cylinders" << endl;
}
Engine::~Engine(){
cout << "Engine was destroyed" << endl;
}
Vehicle.h
:
#ifndef __Test__Vehicle__
#define __Test__Vehicle__
#include <iostream>
#include "Engine/Engine.h"
#include "Tank/Tank.h"
class Vehicle
{
public:
~Vehicle();
protected:
Vehicle(int occ, float tankCapacity, int numCyl);
int occupants;
Engine engine;
Tank tank;
private:
Vehicle();
};
#endif /* defined(__Test__Vehicle__) */
Vehicle.cpp
:
#include <iostream>
#include "Tank/Tank.h"
#include "Engine/Engine.h"
#include "Vehicle.h"
using namespace std;
Vehicle::Vehicle(int occ, float tankCapacity, int numCyl) : engine(numCyl), tank(tankCapacity)
{
this->occupants = occ;
cout << "Vehicle Created" << endl;
}
Thank you for any help that comes my way, any small hint would be appreciated!