using my template class

  • Thread starter Rene Ivon Shamberger
  • Start date
R

Rene Ivon Shamberger

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>::Data() {}

template <class T, const int x>
Data<T, x>::Data(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>::Data(const Data& other) {
//copy ctor
}

int main(){
jme::Data <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::Data'
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.
 
I

Ian Collins

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>::Data() {}

template<class T, const int x>
Data<T, x>::Data(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>::Data(const Data& other) {
//copy ctor
}

There isn't a declaration for this copy constructor in the class.
int main(){
jme::Data<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 said:
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!
 
R

Rene Ivon Shamberger

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::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;
}
 
V

Victor Bazarov

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::Data<T>::Data() {}

Members 'data' and 'id' have been left uninitialized here.
template <class T>
jme::Data<T>::Data(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::Data<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::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;
}

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

jme::Data said:
std::cout << "Hello world!" << std::endl;
return 0;
}

V
 
T

Tobias Müller

Victor Bazarov said:
template <class T> class Data {
private:
T data;
int id;
public:
Data();
Data(T, const int);
virtual ~Data(); [...]
}
template <class T>
jme::Data<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
 
V

Victor Bazarov

Victor Bazarov said:
template <class T> class Data {
private:
T data;
int id;
public:
Data();
Data(T, const int);
virtual ~Data(); [...]
}
template <class T>
jme::Data<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.
 
R

Rene Ivon Shamberger

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 ifyou are able to dupplicate the error, then you will be able to reply to myquestions.
Thanks in advance.
 
I

Ike Naar

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
 
T

Tobias Müller

Rene Ivon Shamberger said:
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
 
J

Juha Nieminen

Ian Collins said:
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).
 
S

Stuart

On 9/29/12 Tobias Müller wrote:
[snip]
I am pretty sure that I did _not_ write that.
[snip]

Is that so? Prove it.

Next time you should add a private key encrypted MD5 sum to your post ;-)

Um, forgot to do so myself ...

Regards,
Stuart
 
T

Tobias Müller

Stuart said:
On 9/29/12 Tobias Müller wrote: [...]
I am pretty sure that I did _not_ write that.
[snip]

Is that so? Prove it.

Next time you should add a private key encrypted MD5 sum to your post ;-)

That would not help much in proving that I didn't write something. It could
be a quote from a different post.
Um, forgot to do so myself ...

My newsreader doesn't even support it.

Tobi
 

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,787
Messages
2,569,630
Members
45,335
Latest member
Tommiesal

Latest Threads

Top