Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > using my template class

Reply
Thread Tools

using my template class

 
 
Rene Ivon Shamberger
Guest
Posts: n/a
 
      09-29-2012
template <class T, const int> class Data {
private:
T data;
int id;
public:
Data();
Data(T, const int);
virtual ~Data();
};
template <class T, const int x>
Data<T, x>:ata() {}

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

template <class T, const int x>
Data<T, x>:ata(const Data& other) {
//copy ctor
}

int main(){
jme:ata <std::string, const int> d("data", 1);
std::cout << "Hello world!" << std::endl;
return 0;
}


The class above compiles, but when trying to use it in the main() function, I get this error:

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

Compiling: main.cpp
C:\...\main.cpp: In function 'int main()':
C:\...\main.cpp:8: error: type/value mismatch at argument 2 in template parameter list for 'template<class T, int <anonymous> > class jme:ata'
C:\...\main.cpp:8: error: expected a constant of type 'int', got 'const int'
C:\...\main.cpp:8: error: invalid type in declaration before '(' token
C:\...\main.cpp:8: error: initializer expression list treated as compound expression
C:\...\main.cpp:8: warning: left-hand operand of comma has no effect
C:\...\main.cpp:8: warning: unused variable 'd'
Process terminated with status 1 (0 minutes, 0 seconds)
4 errors, 2 warnings

I am bit confused as to how to use a class like that.
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      09-29-2012
On 09/29/12 01:22 PM, Rene Ivon Shamberger wrote:
> template<class T, const int> class Data {


Why are you using "const int" here?

> private:
> T data;
> int id;
> public:
> Data();
> Data(T, const int);
> virtual ~Data();
> };
> template<class T, const int x>
> Data<T, x>:ata() {}
>
> template<class T, const int x>
> Data<T, x>:ata(T _data, const int _id) {
> this->data = _data;
> this->id = _id;
> }
> template<class T, const int x>
> Data<T, x>::~Data() {}
>
> template<class T, const int x>
> Data<T, x>:ata(const Data& other) {
> //copy ctor
> }


There isn't a declaration for this copy constructor in the class.

> int main(){
> jme:ata<std::string, const int> d("data", 1);


Two problems I can see:

Where does the namespace jme come from?

The second template argument must be an integer value, not a type.
Something like

Data<std::string, 1> d("data", 1);

> std::cout<< "Hello world!"<< std::endl;
> return 0;
> }
>
>
> The class above compiles, but when trying to use it in the main() function, I get this error:


It won't be compiled until you instantiate it!

--
Ian Collins
 
Reply With Quote
 
 
 
 
Rene Ivon Shamberger
Guest
Posts: n/a
 
      09-29-2012
This is a snip of the original program, but this is the entire program:
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;
}


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      09-29-2012
On 9/29/2012 12:16 AM, Rene Ivon Shamberger wrote:
> This is a snip of the original program, but this is the entire program:
> 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() {}


Members 'data' and 'id' have been left uninitialized here.

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


See FAQ section 10 for recommendations on how to implement constructors.

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


If the destructor doesn't do anything, it's probably better to avoid
declaring and defining it at all.

>
> 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;


When you try to instantiate the template, you must give it some real
arguments, like

jme:ata<int> d;

> std::cout << "Hello world!" << std::endl;
> return 0;
> }
>
>


V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
Tobias Müller
Guest
Posts: n/a
 
      09-29-2012
Victor Bazarov <(E-Mail Removed)> wrote:
> On 9/29/2012 12:16 AM, Rene Ivon Shamberger wrote:
>> template <class T> class Data {
>> private:
>> T data;
>> int id;
>> public:
>> Data();
>> Data(T, const int);
>> virtual ~Data();

[...]
>> }
>> template <class T>
>> jme:ata<T>::~Data() {}

>
> If the destructor doesn't do anything, it's probably better to avoid
> declaring and defining it at all.


Not if it is virtual. If the class contains no other virtual methods this
is not immediately necessary but it's certainly not wrong, especially if
you plan to add virtual methods some time.
If you forget to make the destructor virtual, the derived destructor may
never be called and such bugs are hard to find.
And yes, I know that there are compiler warnings for that...

Tobi
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      09-29-2012
On 9/29/2012 8:49 AM, Tobias Müller wrote:
> Victor Bazarov <(E-Mail Removed)> wrote:
>> On 9/29/2012 12:16 AM, Rene Ivon Shamberger wrote:
>>> template <class T> class Data {
>>> private:
>>> T data;
>>> int id;
>>> public:
>>> Data();
>>> Data(T, const int);
>>> virtual ~Data();

> [...]
>>> }
>>> template <class T>
>>> jme:ata<T>::~Data() {}

>>
>> If the destructor doesn't do anything, it's probably better to avoid
>> declaring and defining it at all.

>
> Not if it is virtual.


Of course. I missed that part.

> If the class contains no other virtual methods this
> is not immediately necessary but it's certainly not wrong, especially if
> you plan to add virtual methods some time.
> If you forget to make the destructor virtual, the derived destructor may
> never be called and such bugs are hard to find.
> And yes, I know that there are compiler warnings for that...
>
> Tobi
>



--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
Rene Ivon Shamberger
Guest
Posts: n/a
 
      09-29-2012
On Saturday, September 29, 2012 8:49:50 AM UTC-4, Tobias Mller wrote:
The information I have presented here is intended to show a problem in the code, the code is, however, fully opperational. Please, try the code and ifyou are able to dupplicate the error, then you will be able to reply to myquestions.
Thanks in advance.
 
Reply With Quote
 
Ike Naar
Guest
Posts: n/a
 
      09-29-2012
On 2012-09-29, Rene Ivon Shamberger <(E-Mail Removed)> wrote:
> On Saturday, September 29, 2012 8:49:50 AM UTC-4, Tobias M?ller wrote:
> The information I have presented here is intended to show a problem in
> the code, the code is, however, fully opperational. Please, try the
> code and if you are able to dupplicate the error, then you will be
> able to reply to my questions.


The code you've shown so far cannot be fully operational, because it
doesn't even compile. If I try the code, the compiler complains
as follows:
a.cpp: In function 'int jme::main()':
a.cpp:49: error: missing template arguments before 'd'
a.cpp:49: error: expected `;' before 'd'
a.cpp:50: error: 'cout' is not a member of 'std'
a.cpp:50: error: 'endl' is not a member of 'std'
a.cpp: At global scope:
a.cpp:52: error: expected `}' at end of input
 
Reply With Quote
 
Tobias Müller
Guest
Posts: n/a
 
      09-29-2012
Rene Ivon Shamberger <(E-Mail Removed)> wrote:
> On Saturday, September 29, 2012 8:49:50 AM UTC-4, Tobias Müller wrote:
> The information I have presented here is intended to show a problem in
> the code, the code is, however, fully opperational. Please, try the code
> and if you are able to dupplicate the error, then you will be able to
> reply to my questions.
> Thanks in advance.


I am pretty sure that I did _not_ write that.
What did you want to cite and how does your "answer" relate to my post?

Tobi
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      10-01-2012
Ian Collins <(E-Mail Removed)> wrote:
> On 09/29/12 01:22 PM, Rene Ivon Shamberger wrote:
>> template<class T, const int> class Data {

>
> Why are you using "const int" here?


The 'const' is indeed rather redundant given that only compile-time
constants are allowed as template parameters (which is even a stricter
requirement than a generic 'const' would be).
 
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
template template arguments: expected a class template, got `Component<T1, T2, T3> gary.bernstein@gmail.com C++ 1 06-08-2007 07:10 AM
using std::map in a template class, getting LNK2019 link error when creating an object from this template class girays C++ 1 01-20-2007 09:47 PM
A parameterized class (i.e. template class / class template) is not a class? christopher diggins C++ 16 05-04-2005 12:26 AM
Using vector of references to a template class in the same template class. Jonathan Bartlett C++ 3 02-08-2005 09:23 PM



Advertisments