Title: Undefined symbols for architecture x86_64 when inheriting
Tags: c++;c++11
Question: I have a C++ code, where I'm trying to use inheritance to reuse my code. But when I make the code I get the following error:

```
  Undefined symbols for architecture x86_64:
    "QuickUnion181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16QuickUnion(int)", referenced from:
        _main in main.o
  ld: symbol(s) not found for architecture x86_64
  
  clang: error: linker command failed with exit code 1 (use -v to see invocation)
  
  make: *** [a.out] Error 1
```

My Makefile:

```CC = g++
CFLAGS = -std=c++11 -Wall -c
LFLAGS = -std=c++11 -Wall
DEBUG = -g
OBJS = main.o unionfind/UnionFind.o unionfind/QuickUnion.o

a.out: $(OBJS)
    $(CC) $(LFLAGS) $(OBJS) $(DEBUG)

main.o: main.cpp
    $(CC) $(CFLAGS) main.cpp $(DEBUG)

unionfind/QuickUnion.o: unionfind/UnionFind.h unionfind/QuickUnion.h unionfind/QuickUnion.cpp
    $(CC) $(CFLAGS) unionfind/QuickUnion.cpp $(DEBUG)

unionfind/UnionFind.o: unionfind/UnionFind.h unionfind/UnionFind.cpp
    $(CC) $(CFLAGS) unionfind/UnionFind.cpp $(DEBUG)
```

The UnionFind .h and .cpp:

```#ifndef UNIONFIND_H
#define UNIONFIND_H

#include <vector&gt;

class UnionFind {
protected:
    int connectedComponents;
    st181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16vector<int&gt; parents;
public:
    UnionFind(int);

    virtual void connect(int, int) = 0;
    virtual int find(int) = 0;
    bool connected(int, int);
    int count();
};

#endif

...

#include "UnionFind.h"

UnionFin181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16UnionFind(int size): parents(size) {
    connectedComponents = size;
    for (int i = 0; i < size; i++) {
        parents[i] = i;
    }
}

bool UnionFin181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16connected(int p, int q) {
    return find(p) == find(q);
}

int UnionFin181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16count() {
    return connectedComponents;
}
```

The QuickUnion .h and .cpp:

```#ifndef QUICKUNION_H
#define QUICKUNION_H

#include <vector&gt;

#include "UnionFind.h"

class QuickUnion: public UnionFind {
public:
    QuickUnion(int);

    void connect(int, int);
    int find(int);
};

#endif

...

#include "QuickUnion.h"

QuickUnion181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16QuickUnion(int size): UnionFind(size) {

}

void QuickUnion181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16onnect(int p, int q) {
    int pRoot = find(p);
    int qRoot = find(q);

    if (pRoot == qRoot) {
        return;
    }

    parents[pRoot] = qRoot;

    connectedComponents--;
}

int QuickUnion181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16ind(int p) {
    while (p != parents[p]) {
        p = parents[p];
    }
    return p;
}
```

main.cpp

```#include <iostream&gt;

#include "unionfind/QuickUnion.h"

using namespace std;

int main() {
    QuickUnion qu(10);

    cout << "Count:" << endl;
    cout << qu.count();
}
```

Maybe there is something to do with the constructor, but I don't know neither why this error is happening nor how to correct it.

PS.: I am using OS X El Capitan.

Comment: Show us `main` method.

Here is the accepted answer: Default output file for command

```g++ -c path/to/file.cpp
```

is not ```path/to/file.o``` but ```./file.o```. That means, that later invoked ```g++``` for linkage of object files fails to find those files. Edit your ```Makefile``` and add ```-o $@``` to ```unionfind/QuickUnion.o``` and ```unionfind/UnionFind.o``` rules.



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2016:00:03/26/16', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c++><c++11>', 'Title': 'Undefined symbols for architecture x86_64 when inheriting', 'ViewCount': 276, 'id': 36233784, 'user_AboutMe': '', 'user_AccountId': 5865787.0, 'user_DisplayName': 'thyago stall', 'user_Reputation': 1626.0, 'user_Views': 220.0, 'user_id': 4619812.0}}