Problem with template class

G

Gaijinco

I'm trying to do a template class Node.

My node.hpp is:

#ifndef _NODE_HPP_
#define _NODE_HPP_

namespace com { namespace mnya { namespace carlos {

template <typename T>
class Node
{
private:
T data;
Node* next;
public:
Node(const T& data);
~Node();
void setData(const T& data);
T getData() const;
void setNext(Node<T>* next);
Node<T>* getNext() const;
};

}}}

#endif

My node.cpp is:

#include "node.hpp"

namespace com { namespace mnya { namespace carlos {

template <typename T>
Node<T>::Node(const T& data):data(data){};

template <typename T>
Node<T>::~Node()
{
if(next!=0)
delete next;
}

template <typename T>
void Node<T>::setData(const T& data)
{
this->data = data;
}

template <typename T>
T Node<T>::getData() const
{
return data;
}

template <typename T>
void Node<T>::setNext(Node<T>* next)
{
this->next = next;
}

template <typename T>
Node<T>* Node<T>::getNext() const
{
return next;
}

}}}

When I try to use the class with:

Node<int> n(10);

The compiler says: "[Linker error] undefined reference to
com::mnya::carlos::Node<int>::Node(int const&)"

What am I doing wrong?

Thanks a lot.
 
P

Pranav

I'm trying to do a template class Node.

My node.hpp is:

#ifndef _NODE_HPP_
#define _NODE_HPP_

namespace com { namespace mnya { namespace carlos {

template <typename T>
class Node
{
private:
T data;
Node* next;
public:
Node(const T& data);
~Node();
void setData(const T& data);
T getData() const;
void setNext(Node<T>* next);
Node<T>* getNext() const;
};

}}}

#endif

My node.cpp is:

#include "node.hpp"

namespace com { namespace mnya { namespace carlos {

template <typename T>
Node<T>::Node(const T& data):data(data){};

template <typename T>
Node<T>::~Node()
{
if(next!=0)
delete next;
}

template <typename T>
void Node<T>::setData(const T& data)
{
this->data = data;
}

template <typename T>
T Node<T>::getData() const
{
return data;
}

template <typename T>
void Node<T>::setNext(Node<T>* next)
{
this->next = next;
}

template <typename T>
Node<T>* Node<T>::getNext() const
{
return next;
}

}}}

When I try to use the class with:

Node<int> n(10);

The compiler says: "[Linker error] undefined reference to
com::mnya::carlos::Node<int>::Node(int const&)"

What am I doing wrong?

Thanks a lot.

I think you are trying declare a namespace twice.. you should declare
a namespace at once, and its contents are used by either resolving the
namespce scope or by 'using' the namespace in your code so that you
can access the contents of namespace,
 
G

Gaijinco

Thanks Pranav.

I changed the code of node.cpp to this, but now I got another compile
error:

#include "node.hpp"

template <typename T>
com::mnya::carlos::Node<T>::Node(const T& data):data(data){};

template <typename T>
com::mnya::carlos::Node<T>::~Node()
{
if(next!=0)
delete next;
}

template <typename T>
void com::mnya::carlos::Node<T>::setData(const T& data)
{
this->data = data;
}

template <typename T>
T com::mnya::carlos::Node<T>::getData() const
{
return data;
}

template <typename T>
void com::mnya::carlos::Node<T>::setNext(Node<T>* next)
{
this->next = next;
}

template <typename T>
Node<T>* com::mnya::carlos::Node<T>::getNext() const
{
return next;
}

The compiler says that in line:

Node<T>* com::mnya::carlos::Node<T>::getNext() const

Expected constructor, destructor, or type conversion before '<' token
Expected ';' before '<' token

Also, I tried to remove the namespaces but that didn't work either!

Thanks again!
 
T

Triple-DES

I'm trying to do a template class Node.

My node.hpp is:

#ifndef _NODE_HPP_
#define _NODE_HPP_

namespace com { namespace mnya { namespace carlos {

  template <typename T>
  class Node
  {
    private:
            T data;
            Node* next;
    public:
           Node(const T& data);
           ~Node();
           void setData(const T& data);
           T getData() const;
           void setNext(Node<T>* next);
           Node<T>* getNext() const;
  };

}}}

#endif

My node.cpp is:

#include "node.hpp"

[definitions]


When I try to use the class with:

Node<int> n(10);

The compiler says: "[Linker error] undefined reference to
com::mnya::carlos::Node<int>::Node(int const&)"

What am I doing wrong?

Thanks a lot.

Instead of #including node.hpp in node.cpp, #include node.cpp at the
bottom of node.hpp. This is a normal way of organizing template source
code.

DP
 
G

Gaijinco

Thanks for pointing the C++ Faq section. I should read it first!
Thanks, now my problem is solved and I know a little bit more about C+
+.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top