Undefined symbols for architecture x86_64

Why is it that every time I try to run my code, I get the following problem?

g++ -Wall -fexceptions -g -Iinclude -c /Users/Soul/Documents/School/Program1/main.cpp -o obj/Debug/main.o
g++ -o bin/Debug/Program1 obj/Debug/main.o obj/Debug/src/Vector.o
Undefined symbols for architecture x86_64:
"Vector<int>::Vector(int, int const&)", referenced from:
_main in main.o
"Vector<int>::~Vector()", 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)

The constructor doesn't seem to work for some reason.

main.cpp
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include "Vector.h"

using namespace std;

int main(){
    Vector<int> testvec(4,5);
    delete testvec.data_val;
    return 0;
}


Vector.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef VECTOR_H
#define VECTOR_H

template <class T>
class Vector{
    public:
        T *data_val;

        Vector();
        Vector(int n,const T& val);
        Vector(const Vector& other);
        virtual ~Vector();
    protected:
    private:
};

#endif // VECTOR_H 


Vector.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "Vector.h"

template <class T>
Vector<T>::Vector(){
    data_val = new T[0];
}

template <class T>
Vector<T>::Vector(int n,const T& val){
    data_val = new T[n];
    for(int index = 0; index < n; index++)
        data_val[index];
}


template <class T>
Vector<T>::Vector(const Vector& other){
    //copy ctor
}

template <class T>
Vector<T>::~Vector(){
    delete data_val;
}
Last edited on
Try putting the implementations into the header file. When dealing with template classes both the implementation and definition should be in the same file.

I did that, but the constructor won't allocate memory for dataval. When the program runs this is my output:

Last login: Mon Sep 28 11:03:30 on ttys000
/Applications/CodeBlocks.app/Contents/MacOS/cb_console_runner DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:. /Users/Soul/Documents/School/Program1/bin/Debug/Program1
Christians-MBP:~ Soul$ /Applications/CodeBlocks.app/Contents/MacOS/cb_console_runner DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:. /Users/Soul/Documents/School/Program1/bin/Debug/Program1
Program1(4716,0x7fff786d0300) malloc: *** error for object 0x7fdef3404af0: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
0
Process returned -1 (0xFFFFFFFF) execution time : 0.294 s
Press ENTER to continue.

Updated main.cpp
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include "Vector.h"

using namespace std;

int main(){
    Vector<int> testvec(4,5);
    cout << testvec.dataval[2];
    delete testvec.dataval;
    return 0;
}


Updated Vector.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#ifndef VECTOR_H
#define VECTOR_H

template <class T>
class Vector{
    public:
        T *dataval;

        Vector();
        Vector(int n,const T& val);
        Vector(const Vector& other);
        virtual ~Vector();
    protected:
    private:
};

//Imlementations
template <class T>
Vector<T>::Vector(){
    dataval = new T[0];
}

template <class T>
Vector<T>::Vector(int n,const T& val){
    dataval = new T[n];
    for(int index = 0; index < n; index++)
        dataval[index];
}


template <class T>
Vector<T>::Vector(const Vector& other){
    //copy ctor
}

template <class T>
Vector<T>::~Vector(){
    delete dataval;
}

#endif // VECTOR_H 
Try removing the delete testvec.dataval; from main(), let your destructor do it's job.
Thanks, you're a lifesaver
Topic archived. No new replies allowed.