I have the following code and I am getting these errors:
"/tmp/ccymmVdG.o: In function `main':
1main.cpp:(.text+0x30): undefined reference to `unique_int_ptr::unique_int_ptr(int*)"
unique_int.hpp
#ifndef UNIQUE_PTR_HPP_INCLUDED
#define UNIQUE_PTR_HPP_INCLUDED
#include <typeinfo>
template<class Template> class unique_ptr
{
public:
unique_ptr(Template* ptr);
~ unique_ptr();
Template* get();
Template* operator->();
Template& operator*();
private:
Template* m_ptr;
/* unique_ptr(unique_ptr<Template> const&);
unique_ptr& operator=(const unique_ptr&);*/
};
#endif /* UNIQUE_PTR_HPP_INCLUDED */
unique_int_ptr.hpp
#ifndef UNIQUE_PTR_INT_HPP_INCLUDED
#define UNIQUE_PTR_INT_HPP_INCLUDED
class unique_int_ptr
{
public:
int* get();
unique_int_ptr (int* data);
~unique_int_ptr ();
private:
int* m_ptr;
};
#endif //UNIQUE_PTR_INT_HPP_INCLUDED
unique_ptr.cpp
#include <stdio.h>
#include "unique_ptr.hpp"
template<typename Template>
unique_ptr<Template>::unique_ptr(Template* ptr)
{
m_ptr = ptr;
}
template<typename Template>
unique_ptr<Template>::~unique_ptr()
{
delete m_ptr;
}
template<typename Template>
Template* unique_ptr<Template>::get()
{
return m_ptr;
}
template<typename Template>
Template* unique_ptr<Template>:: operator ->()
{
return m_ptr;
}
template<typename Template>
Template& unique_ptr<Template>::operator *()
{
return *m_ptr;
}
unique_int_ptr.cpp
#include "unique_int_ptr.hpp"
#include <stdio.h>
unique_int_ptr::unique_int_ptr (int* data)
{
m_ptr = new int;
m_ptr = data;
}
unique_int_ptr::~unique_int_ptr()
{
delete m_ptr;
}
int* unique_int_ptr::get ()
{
return m_ptr;
}
1main.cpp
#include "unique_ptr.hpp"
#include "unique_int_ptr.hpp"
#include <stdio.h>
#include <iostream>
#define TEST_TRUE(x) printf("Test '%s' %s\n", #x, (x) ? "passed" : "failed!")
struct SimpleData
{
int first;
char second;
};
this is my main function
int main()
{
{
int* data = new int(55); // allocate raw pointer
// create a managed pointer and let it manage data
unique_int_ptr data_ptr(data);
// Make sure that both pointers show the same value
TEST_TRUE(*data == *data_ptr.get());
// Both ways
*(data_ptr.get()) = 99;
TEST_TRUE(*data == *data_ptr.get());
};
{
int* data = new int(55); // allocate raw pointer
// create a managed pointer and let it manage data
unique_ptr<int> data_ptr(data);
// Make sure that both pointers show the same value
TEST_TRUE(*data == *data_ptr.get());
// Both ways
*(data_ptr.get()) = 99;
TEST_TRUE(*data == *data_ptr.get());
};
{
SimpleData* data = new SimpleData; // allocate raw pointer
data->first = 55;
data->second = 'C';
// create a managed pointer and let it manage data
unique_ptr<SimpleData> data_ptr(data);
// Make sure that both pointers show the same value
TEST_TRUE(data->first == data_ptr->first);
TEST_TRUE(data->second == data_ptr->second);
// Both ways
data_ptr->first = 99;
data_ptr->second = 'D';
TEST_TRUE(data->first == data_ptr->first);
TEST_TRUE(data->second == data_ptr->second);
unique_ptr<int> int_ptr(new int);
*int_ptr = 199;
// both allocations are released here
}
return 0;
}
Do you have any idea why I am getting that error? This error appears several times. I looked at other examples about templates but it does not work.