1

The error that I'm getting is this:

||=== Build: Debug in Building Sim (compiler: GNU GCC Compiler) ===|
obj/Debug/main.o||In function `__static_initialization_and_destruction_0':|
/home/josh/Documents/cpp/building_sim/main.cpp|15|undefined reference to `User::~User()'|
/home/josh/Documents/cpp/building_sim/main.cpp|20|undefined reference to `User::~User()'|
/home/josh/Documents/cpp/building_sim/main.cpp|20|undefined reference to `Building::~Building()'|
/home/josh/Documents/cpp/building_sim/main.cpp|20|undefined reference to `User::~User()'|
||=== Build failed: 4 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

I've been having this error for a few days now and have seen a good 14-18 threads that have answers to this issue that do absolutely nothing for mine. At this point my project is made up of five files:

main.cpp
user.hpp
user.cpp
building.hpp
building.cpp

main.cpp links building.hpp and user.hpp, building and user.cpp link their respective header files and building.cpp links user.hpp.

I've tried building this project a multitude of times two different ways. I created a shell script to build all three cpp files to .o files and then join them (in all possible orders), and I tried simply g++ main.cpp building.cpp user.cpp in all possible orders and I created a Code::Blocks project to link all the files together manually. All of my header files have include guards, but I don't think that's an issue.

The full text of my files follows.

main.cpp

#include <stdio.h>
#include "building.hpp"
#include "user.hpp"

// TODO Text parsing
// TODO Resources
// TODO Generations

using namespace std;

//----------------------------------
//            Variables
//----------------------------------
//-----User------
User user(1000);

//---Buildings---
int numBuildings = 0;

Building building1(user, 10, 14, "George");
Building buildings[] = {building1};

int main(){

    return 0;
}

user.hpp

#include <stdio.h>

#ifndef user_h
#define user_h

class User{
public:
    User();
    ~User();

    User(int _money);

    int getMoney();
    void setMoney(int _money);

    int getBuildings();
    void setBuildings(int _buildings);

private:
    int money;
    int buildings;
};

#endif

user.cpp

#include "user.hpp"

User::User(){
    User::money = 1000;
    User::buildings = 0;
}

User::User(int _money){
    User::money = _money;
    setBuildings(getBuildings()+1);
}

int User::getMoney(){
    return User::money;
}

void User::setMoney(int _money){
    User::money = _money;
}

int User::getBuildings(){
    return User::buildings;
}

void User::setBuildings(int _buildings){
    User::buildings = _buildings;
}

building.hpp

#include <iostream>
#include <string>
#include "user.hpp"

#ifndef building_h
#define building_h

class Building{
public:
    Building();
    ~Building();

    Building(User user);
    Building(User user, int _cost, int _payout);
    Building(User user, int _cost, int _payout, std::string _name);

    int getCost();
    int getPayout();

private:
    int cost;
    int payout;
    std::string name;
};

#endif

building.cpp

#include "building.hpp"

Building::Building(){
    std::cout << "\n[ERROR] Default constructor used, no building created.\n";
}

Building::Building(User user){
    Building::cost = 10;
    Building::payout = 20;
    Building::name = "Generic";
    user.setBuildings(user.getBuildings()+1);
}

Building::Building(User user, int _cost, int _payout){
    Building::cost = _cost;
    Building::payout = _payout;
    Building::name = "Generic";
    user.setBuildings(user.getBuildings()+1);
}

Building::Building(User user, int _cost, int _payout, std::string _name){
    Building::cost = _cost;
    Building::payout = _payout;
    Building::name = _name;
    user.setBuildings(user.getBuildings()+1);
}

int Building::getCost(){
    return Building::cost;
}

int Building::getPayout(){
    return Building::payout;
}
theStandard
  • 212
  • 2
  • 9
  • 7
    The error says nothing about constructors. Destructors, on the other hand, you have not defined. – chris Apr 21 '14 at 21:42
  • 1
    Agree with @chris, the destructor is defined but not implemented, that's why you get the error. – vsoftco Apr 21 '14 at 21:44
  • I removed those because I was told that the compiler would automatically generate default destructors. (If this turns out to be what I've been losing my mind over for a few days now I will definitely be very embarrassed) – theStandard Apr 21 '14 at 21:44
  • 1
    It does, unless you explicitly *declare* that you're going to define them. (So you just need to remove your destructor declarations.) – Lilshieste Apr 21 '14 at 21:47
  • Well this is insanely embarrassing. Should I leave this question open? – theStandard Apr 21 '14 at 21:51
  • 1
    @vsoftco, Defined and implemented are pretty much the same term when it comes to C++ functions. A better choice for your usage of defined would be declared. – chris Apr 21 '14 at 21:54
  • 1
    Leave it open. It may be embarrassing, but we've all been there at some point in our lives. – Lilshieste Apr 21 '14 at 21:55
  • @chris, yes I meant declared (as in the .h file) vs implemented (as in the .cpp file) – vsoftco Apr 21 '14 at 22:23

1 Answers1

2

You simply forgot to define the both destructors: User and Building in corresponding cpp modules.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Yeah the comment on my post clarified that. It's a little embarrassing considering I probably thought I'd get around to it and then never did. Thanks. – theStandard Apr 21 '14 at 21:54