Template Class

  • Thread starter Rene Ivon Shamberger
  • Start date
R

Rene Ivon Shamberger

namespace jme {
template <class T> class Data {
private:
T data;
int id;
public:
Data();
Data(T, const int);
virtual ~Data();
Data(const Data& other);
Data& operator=(const Data& other);
const int getId();
const T& getData() {
return data;
}
};

template <class T>
jme::Data<T>::Data() {}

template <class T>
jme::Data<T>::Data(T _data, const int _id) {
this->data = _data;
this->id = _id;
}
template <class T>
jme::Data<T>::~Data() {}

template <class T>
jme::Data<T>::Data(const Data& other) {
//copy ctor
}
template <class T>
jme::Data<T>&
jme::Data<T>::eek:perator=(const Data& rhs) {
//if (this == &rhs) return *this; // handle self assignment
//assignment operator
this->T = rhs.T;
//this->id = rhs.id;
return *this;
}
template <class T>
const int jme::Data<T>::getId() {
return id;
}
---
int main(){
jme::Data d;
std::cout << "Hello world!" << std::endl;
return 0;
}

The above program gives me this error message:

-------------- Build: Debug in data ---------------

Linking console executable: bin\Debug\list.exe
obj\Debug\main.o: In function `main':
C:/.../main.cpp:8: undefined reference to `jme::Data<std::string>::Data()'

C:/.../main.cpp:10: undefined reference to `jme::Data<std::string>::~Data()'

C:/.../main.cpp:10: undefined reference to `jme::Data<std::string>::~Data()'

collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 0 seconds)
3 errors, 0 warnings



What am I doing wrong?
 
I

Ian Collins

On 09/29/12 16:20, Rene Ivon Shamberger wrote:

<snip>

What are you trying to do? You keep posting incomplete snippets with
errors that don't match the code.

jme::Data is a template, you have to provide an argument to instantiate it.
std::cout<< "Hello world!"<< std::endl;
return 0;
}

The above program gives me this error message:

-------------- Build: Debug in data ---------------

Linking console executable: bin\Debug\list.exe
obj\Debug\main.o: In function `main':
C:/.../main.cpp:8: undefined reference to `jme::Data<std::string>::Data()'

I very much doubt what you posted cased this error.
What am I doing wrong?

Not posting the code to match your errors.
 
R

Rui Maciel

Rene said:
The above program gives me this error message:

No, it doesn't. But it still has a hand full of errors, such as:

- you called std::cout and friends, but you haven't include iostream
- the declaration of namespace jme is missing a closing tag
- you tried to declare an object based on class jme::Data without specifying
the type of typename T


Rui Maciel
 
R

Rene Ivon Shamberger

This code help you?
I just might be that I tired, but I cannot see anything wrong in this code.
template <class T> class Data{
private:
T data;
public:
Data();
Data(T);
virtual ~Data();
};
template <class T>
jme::Data<T>::Data() {}

template <class T>
jme::Data<T>::Data(T _data) {
this->data = _data;
}
template <class T>
jme::Data<T>::~Data() {}
..............
using namespace std;
int main() {
jme::Data<int> d(1); // this does not work

jme::Data<int> d; // this does not work
cout << "Hello world!" << endl;
return 0;
}
 
V

Victor Bazarov

This code help you?
I just might be that I tired, but I cannot see anything wrong in this code.

I think you need to take a break from that code.

Only a couple of things are wrong. (A) 'jme' is undefined. You've been
told it already, but you don't seem to pay enough attention, which
suggests that you need to have a fresh start.

Fix: Remove all instances of "jme::" from this program.

(B) 'cout' is undefined.

Fix: Add #include <iostream> to the top of the code.

(C) There are two variables named 'd' in the 'main' function.

Fix: Rename one of them.

After that your program will compile. Will that help? I am not sure.
Again, you definitely *need a break*. Use this opportunity, go out, get
a rest. Get drunk, if that helps. Get laid. Just get your mind off
that program - it's no use keeping at it if your brain has stopped working.
template <class T> class Data{
private:
T data;
public:
Data();
Data(T);
virtual ~Data();
};
template <class T>
jme::Data<T>::Data() {}

template <class T>
jme::Data<T>::Data(T _data) {
this->data = _data;
}
template <class T>
jme::Data<T>::~Data() {}
.............
using namespace std;
int main() {
jme::Data<int> d(1); // this does not work

jme::Data<int> d; // this does not work
cout << "Hello world!" << endl;
return 0;
}

V
 

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

Forum statistics

Threads
473,757
Messages
2,569,543
Members
45,026
Latest member
camilin05

Latest Threads

Top