开发者

linker stage: undefined reference

I already read like 15 of开发者_如何学C the undefined reference topics here and used google too to find a valid answer, but there was non that really helped.

I wrote a 1D and 2D simple storage class which allows rapid loop through by directly accessing the data (plus some convenience accessors). Here the 1D goes (2D is pretty much the same, just 2D)

Header:

#ifndef MULTI1_H
#define MULTI1_H
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>

template <class T>
class Multi1
{
public:
    Multi1(int32_t size_ = -1);
    ~Multi1();
    void set(T val, int32_t pos);
    T& operator[] (int32_t pos) const;
    T& at(int32_t pos) const;
    int32_t m_size;
    T *m_data;
};

#endif // MULTI1_H

Body:

#include "multi1.h"

template <class T>
Multi1<T>::Multi1(int32_t size_) :
  m_size(size_),
  m_data(NULL)
{
  if (size_>0)
    m_data = (T *) malloc (sizeof(T)*size_);
  else
     m_size = 0;
}

template <class T>
Multi1<T>::~Multi1()
{
  if (m_data!=NULL)
    free (m_data);
}

template <class T>
void Multi1<T>::set(T val, int32_t pos)
{
  m_data[pos] = val;
}

template <class T>
T& Multi1<T>::operator[](int32_t pos) const
{
  return m_data[pos];
}

template <class T>
T& Multi1<T>::at(int32_t pos) const
{
  return m_data[pos];
}


./build/main.o: In function `setupDemo(Multi1<voCam*>&, Multi1<voBoard*>&, Multi2<voBoardObservation*>&)':
./src/main.cpp:32: undefined reference to `Multi1<voCam*>::operator[](int) const'
./src/main.cpp:31: undefined reference to `Multi1<voCam*>::operator[](int) const'
./src/main.cpp:30: undefined reference to `Multi1<voBoard*>::operator[](int) const'
./src/main.cpp:29: undefined reference to `Multi1<voBoard*>::operator[](int) const'
./src/main.cpp:41: undefined reference to `Multi1<voCam*>::operator[](int) const'
....

I checked the includes like 5 times, and I recently got pretty used to C programming where I can keep things like linker vendetta from my neck.

Note: I am compiling with g++, linker flags are -lstdc++ -lm


Did you put your template functions inside the header file?

So template <class T> void Multi1<T>::set(T val, int32_t pos) { m_data[pos] = val; }

should be inside your multi.h header file.


Where have you defined?

Multi1<voCam*>::operator[](int) const

Ah ok, You have it, You should have template declaration and definition in the same header file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜