3 Replies - 246 Views - Last Post: 08 October 2011 - 08:35 AM Rate Topic: -----

#1 giocarmine  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 15-September 10

Question about generic types

Posted 06 October 2011 - 09:00 AM

Hello all,

i'm doing some modification in a C++ program.
What i need to do is to have an object that stores an enumeration, but i don't know what this enumeration is.
So i tried to write a template to store a generic enumeration like this:


template <class T = DefaultEnum>

class MyTemplate {

protected:
  T *next;

public:

  void setNext(T *m) {
      next = m;
  }

  T* getNext() {
      return next;
  }

};





This will be stored in another object with methods to access it:


class MyClass {
private:
    MyTemplate<> *mt;


   MyTemplate<> *getN() {
      return n;
  }

  void setN(MyTemplate<> *m) {
      next = m;
  }






I used <> because i thought that in this way i could use some enumeration that it isn't the default one.
So i tried to put into MyTemplate another enumeration, say MyEnum.


  MyClass mc;

  MyEnum initial = SP;
  MyTemplate<MyEnum> *mt = new Mytemplate<MyEnum>();
  mt->setNext(&initial);

  mc.setN(mt);






But i get this error:
no matching function for call to ‘MyClass::setN(MyTemplate<MyEnum>*&)’
candidate is: void 'MyClass::setN(MyTemplate<>*)

I don't know how to pass such enumeration to MyClass and i didn't find something that could help me to understand..
I would like to know how can i solve this problem, even if someone can give me some advice with a different solution it would be very helpful.

Is This A Good Question/Topic? 0
  • +

Replies To: Question about generic types

#2 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2151
  • View blog
  • Posts: 3,307
  • Joined: 21-June 11

Re: Question about generic types

Posted 06 October 2011 - 02:43 PM

Writing MyTemplate<> is the same as MyTemplate<DefaultEnum> (the same way that when you declare a function f as int f(int x=42), writing f() is the same as f(42)).

If you want to work it for all types, you need to make setN as a template function like this: template <T> void setN(MyTemplate<T> *m).
Was This Post Helpful? 0
  • +
  • -

#3 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 450
  • View blog
  • Posts: 849
  • Joined: 17-March 11

Re: Question about generic types

Posted 06 October 2011 - 08:36 PM

In fact you should make the whole of MyClass again a template class.

template <class T = DefaultEnum>
class MyClass {
  private:
   MyTemplate<T> *mt;


   MyTemplate<T> *getN() {
      return n;
  }

  void setN(MyTemplate<T> *m) {
      next = m;
  }
};





[/code]
Was This Post Helpful? 1
  • +
  • -

#4 giocarmine  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 15-September 10

Re: Question about generic types

Posted 08 October 2011 - 08:35 AM

Thanks a lot for your clarifications :)
in the end i needed to choose a different way to make this work.. :P
but your advices will be userful in the future ^_^
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1