Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Template Class

Reply
Thread Tools

Template Class

 
 
Rene Ivon Shamberger
Guest
Posts: n/a
 
      09-29-2012
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:ata<T>:ata() {}

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

template <class T>
jme:ata<T>:ata(const Data& other) {
//copy ctor
}
template <class T>
jme:ata<T>&
jme:ata<T>: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:ata<T>::getId() {
return id;
}
---
int main(){
jme:ata 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:ata<std::string>:ata()'

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

C:/.../main.cpp:10: undefined reference to `jme:ata<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?
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      09-29-2012
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:ata d;


jme:ata 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:ata<std::string>:ata()'


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
 
Reply With Quote
 
 
 
 
Rui Maciel
Guest
Posts: n/a
 
      09-29-2012
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:ata without specifying
the type of typename T


Rui Maciel
 
Reply With Quote
 
Rene Ivon Shamberger
Guest
Posts: n/a
 
      09-29-2012
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:ata<T>:ata() {}

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

jme:ata<int> d; // this does not work
cout << "Hello world!" << endl;
return 0;
}
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      09-29-2012
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:ata<T>:ata() {}
>
> template <class T>
> jme:ata<T>:ata(T _data) {
> this->data = _data;
> }
> template <class T>
> jme:ata<T>::~Data() {}
> .............
> using namespace std;
> int main() {
> jme:ata<int> d(1); // this does not work
>
> jme:ata<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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Declaring a template class with two template params a friend in anon-template class A L C++ 1 08-25-2010 07:25 AM
r H2 deduce deduce template argument of a template class inheritingfrom a non template base? nguillot C++ 5 03-08-2009 05:56 PM
How to use the template member function of a template in the memberfunction of another template class? Peng Yu C++ 3 10-26-2008 03:51 PM
template template arguments: expected a class template, got `Component<T1, T2, T3> gary.bernstein@gmail.com C++ 1 06-08-2007 07:10 AM
A parameterized class (i.e. template class / class template) is not a class? christopher diggins C++ 16 05-04-2005 12:26 AM



Advertisments