链接器阶段:未定义的引用

发布于 2024-11-23 16:23:23 字数 1891 浏览 1 评论 0原文

我已经在这里阅读了 15 个未定义的参考主题,并使用谷歌找到了有效的答案,但没有真正有帮助的。

我编写了一个一维和二维简单存储类,它允许通过直接访问数据(加上一些方便的访问器)来快速循环。这里是 1D(2D 几乎相同,只是 2D)

标题:

#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

正文:

#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'
....

我检查了包含大约 5 次,我最近非常习惯 C 编程,在那里我可以避免像链接器仇杀这样的事情。

注意:我使用 g++ 进行编译,链接器标志为 -lstdc++ -lm

I already read like 15 of 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

五里雾 2024-11-30 16:23:23

您是否将模板函数放入头文件中?

所以<代码>
模板
void Multi1::set(T val, int32_t pos)
{
m_data[位置] = val;
}

应该位于您的 multi.h 头文件中。

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.

后知后觉 2024-11-30 16:23:23

你在哪里定义的?

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

好吧,你已经有了,你应该在同一个头文件中有模板声明和定义。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文