Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why getting linker error with vector?

I have for homework to write my own abstract class Vector. I make some of the code, but when I try to compile it I have error. This is the code:

vector.hh:

#ifndef VECTOR__HH__
#define VECTOR__HH_

template<class T> class Vector {
    int capacity_;
    int size_;
    T* buffer_;

    void ensure_capacity(unsigned size);

public:
    Vector(int capacity=10) 
      : capacity_(capacity), size_(0), buffer_(new T[capacity])
    { }

    ~Vector() {
        delete []buffer_;
    }

    int size() const {
        return size_;
    }

    bool empty() const {
        return size_ == 0;
    }

    T& operator[](int n) {
        return buffer_[n];
    }

    const T& operator[](int n) const {
        return buffer_[n];
    }

    void clear() {
        // TODO
    }

    int capacity() const {
        return capacity_;
    }

    T& front() {
        return buffer_[0];
    }

    const T& front() const {
        return buffer_[0];
    }

    T& back() {
        return buffer_[size_-1];
    }

    const T& back() const {
        return buffer_[size_-1];
    }

    void push_back(const T& value);
};
#endif

vector.cc:

#include "vector.hh"

template<class T> 
void Vector<T>::ensure_capacity(unsigned size) {
    if(capacity_>size+1) {
        return;
    } 

    capacity_ = capacity_ ==0?1:capacity_;

    while(capacity_<size+1) {
        capacity_*=2;
    }

    T* old_buffer = buffer_;
    buffer_ = new T[capacity_];
    memcpy(buffer_, old_buffer, sizeof(T)*size_);

    delete [] old_buffer;
}

template<class T>
void Vector<T>::push_back(const T& value) {
    ensure_capacity(size()+1);

    buffer_[size_] = value;
    size_++;
    buffer_[size_] = '/0';
}

main.cc:

#include "vector.hh"
#include <iostream>
using namespace std;

int main(int argc, char* argv[]) {
    Vector<int> v(2);
    v.push_back(10);

    cout << v[0];

    return 0;
}

And error is:

g++ -c -o main.o main.cc
g++ -Wall -g vector.o main.o -o hw02vector
main.o: In function 'main':
main.cc:(.text+0x37): undefined reference to 'Vector<int>::push_back(int const&)'
collect2: ld returned 1 exit status
make: * [hw02vector] Error 1

like image 396
nyanev Avatar asked Apr 30 '26 19:04

nyanev


1 Answers

The linker error is coming because, for template classes the definition should always be visible. Either you can move all the content of vector.cc into vector.h. Or you can simply include vector.cc wherever you include vector.h.

side note:

Following lines in your .h file doesn't help:

#ifndef VECTOR__HH__
#define VECTOR__HH_

make both the macros similar such as, VECTOR__HH. This macros are used to avoid multiple inclusion of files.

like image 119
iammilind Avatar answered May 03 '26 08:05

iammilind



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!