链接器阶段:未定义的引用
我已经在这里阅读了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否将模板函数放入头文件中?
所以<代码>::set(T val, int32_t pos)
模板
void Multi1
{
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.
你在哪里定义的?
好吧,你已经有了,你应该在同一个头文件中有模板声明和定义。
Where have you defined?
Ah ok, You have it, You should have template declaration and definition in the same header file.