2

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!

Biffen
  • 6,249
  • 6
  • 28
  • 36
user3555373
  • 149
  • 1
  • 8
  • 3
    Looks like you're not compiling/linking `Tank.cpp`. – Biffen May 07 '15 at 09:22
  • @Biffen, you're my hero tonight. I manually `#included` the .cpp files and everything went through as it should have. Feel pretty silly, but I guess this is how you learn. Thank you once again! – user3555373 May 07 '15 at 09:36
  • 3
    **`#include`ing `.cpp` files is *not* the solution!** They should probably be added to the project somehow. Perhaps someone with more Xcode experience than myself could tell you exactly how. – Biffen May 07 '15 at 09:38
  • Off-topic, but you shouldn't be using [reserved names](http://stackoverflow.com/questions/228783) like `__Test__Tank__`. And you definitely shouldn't `#include` source files; they need to be separately compiled and linked. – Mike Seymour May 07 '15 at 10:15

0 Answers0