Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Template Class (http://www.velocityreviews.com/forums/t952825-template-class.html)

Rene Ivon Shamberger 09-29-2012 04:20 AM

Template Class
 
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>::operator=(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?

Ian Collins 09-29-2012 04:37 AM

Re: Template Class
 
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.

> ---
> int main(){
> jme::Data d;


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.

--
Ian Collins

Rui Maciel 09-29-2012 09:03 AM

Re: Template Class
 
Rene Ivon Shamberger wrote:

> 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

Rene Ivon Shamberger 09-29-2012 02:14 PM

Re: Template Class
 
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;
}

Victor Bazarov 09-29-2012 02:55 PM

Re: Template Class
 
On 9/29/2012 10:14 AM, Rene Ivon Shamberger wrote:
> 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
--
I do not respond to top-posted replies, please don't ask


All times are GMT. The time now is 04:33 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.