Like Tree1Likes

Template vectors

This is a discussion on Template vectors within the C++ Programming forums, part of the General Programming Boards category; I tried this: Code: #include <iostream> #include <vector> using namespace std; template <class MyDummyClass, typename ReturnType, typename Parameter> class SingularCallBack ...

  1. #1
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115

    Template vectors

    I tried this:
    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    template <class MyDummyClass, typename ReturnType, typename Parameter>
    class SingularCallBack
    {
    public:
    	  typedef ReturnType (MyDummyClass ::*Method)(Parameter);
    	  
    	  SingularCallBack(MyDummyClass* _class_instance, Method _method)
    	  {
    	       class_instance = _class_instance;
    	       method             = _method;
    	  };
    
    	  ReturnType execute(Parameter parameter)
    	  {
    	       return (class_instance->*method)(parameter);
    	  };
    
    private:
    	  MyDummyClass*   class_instance;
    	  Method  method;
    };
    
    
    class BaseClass
    {
       public:
         virtual bool DerivedMethod (const std::string& str)
         {
    	  return true; 
         }
    };
    
    class AClass : public BaseClass
    {
    public:
      template < class derivedComponent, class Allocator = allocator<derivedComponent> > class callbackList;
         	  
      AClass ()  {}
     
      bool AMethod( std::string& str)
      {
         std::cout << "AClass[]: " << str << std::endl;
         return true;
      }
    };
    
    int main()
    {     
       AClass a ();   
       a.callbackList.push_back (SingularCallBack < AClass, bool, const std::string& > (&a, &AClass :: AMethod, "sadsa"));
    
       return 0;
    }
    Below is the compilation error:
    Code:
    anisha@linux-uitj:~> g++ callback3.cpp -Wall -Wextra
    callback3.cpp:31: warning: unused parameter ‘str’
    callback3.cpp: In function ‘int main()’:
    callback3.cpp:54: error: request for member ‘callbackList’ in ‘a’, which is of non-class type ‘AClass()’
    callback3.cpp:54: error: no matching function for call to ‘SingularCallBack<AClass, bool, const std::string&>::SingularCallBack(AClass (*)(), bool (AClass::*)(std::string&), const char [6])’
    callback3.cpp:11: note: candidates are: SingularCallBack<MyDummyClass, ReturnType, Parameter>::SingularCallBack(MyDummyClass*, ReturnType (MyDummyClass::*)(Parameter)) [with MyDummyClass = AClass, ReturnType = bool, Parameter = const std::string&]
    callback3.cpp:7: note:                 SingularCallBack<AClass, bool, const std::string&>::SingularCallBack(const SingularCallBack<AClass, bool, const std::string&>&)
    Please help.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    You're basically forward declaring CallbackList here, so of course there is no such member.
    Did you intend to create an instance of it instead?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It already begins with this:

    Code:
    AClass a ();
    This is a function declaration. To create a default-constructed object use

    Code:
    AClass a;
    Next, AClass does not contain a (vector?) member called callbackList.

    And finally, SingularCallBack does not have a contructor that takes those 3 arguments. The last, a string, is supposed to be provided when you call the execute method.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115
    Thank you Elysia and anon.

    Quote Originally Posted by anon View Post
    It already begins with this:
    Code:
    AClass a ();
    This is a function declaration. To create a default-constructed object use
    Thanks for pointing out, it was not a deliberate fault from my side.

    Quote Originally Posted by anon View Post
    And finally, SingularCallBack does not have a contructor that takes those 3 arguments. The last, a string, is supposed to be provided when you call the execute method.
    I have corrected that too now, but the error persists.

    Quote Originally Posted by anon View Post
    Next, AClass does not contain a (vector?) member called callbackList..
    It seems I haven't yet understood the concepts well, I want to declare and define a vector which
    contains the parameters of the Singular Class, for that I looked up this link: vector - C++ Reference

    The following statement in the above code is from that link:
    Code:
    template < class T, class Allocator = allocator<T> > class vector;
    Quote Originally Posted by Elysia View Post
    You're basically forward declaring CallbackList here, so of course there is no such member.
    I did put CallbackList in class AClass,

    I know I am barking up the wrong tree, please make me understand what I am missing?
    Last edited by AnishaKaul; 03-09-2011 at 10:54 PM.

  5. #5
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115
    I think this makes some sense now:
    Code:
    template<typename t>
    class stuff {
         public:
    	vector<t> data;
    };
    
    
    class BaseClass
    {
       public:     
         typedef stuff < int > ss;  
    
         virtual bool DerivedMethod (const std::string& str)
         {
    	  return true; 
         }
    };
    The RED statement is correct, but now instead of the type "int" I need to pass the "SingularCallBack" class to it.

    I don't know how to do that, Please help.
    Last edited by AnishaKaul; 03-10-2011 at 02:27 AM.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    template < class T, class Allocator = allocator<T> > class vector;
    This is the declaration of vector, ie how the vector class looks like. If you want to create a vector, you would do something like
    std::vector<SingularCallBack> myvector;
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115
    I ve already tried that Elysia, that doesn't work:
    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    template <class MyDummyClass, typename ReturnType, typename Parameter>
    class SingularCallBack
    {
    public:
    	  typedef ReturnType (MyDummyClass ::*Method)(Parameter);
    	  
    	  SingularCallBack(MyDummyClass* _class_instance, Method _method, Parameter x)
    	  {
    	       class_instance = _class_instance;
    	       method             = _method;
    	  };
    
    	  ReturnType execute(Parameter parameter)
    	  {
    	       return (class_instance->*method)(parameter);
    	  };
    
    private:
    	  MyDummyClass*   class_instance;
    	  Method  method;
    };
    
    template<class t>
    class stuff {
         public:
    	vector<t> data;
    };
    
    
    class BaseClass
    {
       public:
         
         std::vector<SingularCallBack> myvector;	     
    };
    
    class AClass : public BaseClass
    {
    public:          	       
      AClass ()  {       
      }
      
    };
    
    int main()
    {     
       return 0;
    }
    Error:
    Code:
    anisha@linux-uitj:~> g++ callback3.cpp
    callback3.cpp:38: error: type/value mismatch at argument 1 in template parameter list for ‘template<class _Tp, class _Alloc> class std::vector’
    callback3.cpp:38: error:   expected a type, got ‘SingularCallBack’
    callback3.cpp:38: error: template argument 2 is invalid
    anisha@linux-uitj:~>

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    OK, that was bad of me.
    SingularCallBack is a template class, and therefore not technically a type.
    So you'll have to properly specialize SingularCallBack, like so:
    std::vector< SingularCallBack<T1, T2, T3> > myvector;
    Where T1, T2, T3 are types you want to pass to SingularCallBack. They may be whatever you want.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115
    Actually, that works if I specify the type in the base class, but the problem is that the type of the first argument will be decided by the derived class, using a template means that we can pass a type at the run time. If so, then how to
    accomplish that?

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    Actually, templates pass types at compile time.
    Anyhow, I'm not sure what you want to do, but here is an example:
    Code:
    template <class MyDummyClass, typename ReturnType, typename Parameter>
    class BaseClass
    {
    public:
    	std::vector< SingularCallBack<MyDummyClass, ReturnType, Parameter> > myvector;	     
    };
    
    template <class MyDummyClass, typename ReturnType, typename Parameter>
    class AClass : public BaseClass<MyDummyClass, ReturnType, Parameter>
    {
    public:          	       
    	AClass () 
    	{
    	}
    };
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115
    Yes you are right, I explained it wrongly, templates do pass types at compile time,

    What I want to do is:

    The base class which contains the vector, will have various derived classes and the first parameter of the
    SingularCallBack class is the object of a derived class, so the object and the desired function to be set as callback is to be put in that vector by the derived class.

    I tried your code in my already existent classes which have some functions already defined, I got this error:
    Code:
     error: ‘template<class MyDummyClass, class ReturnType, class Parameter> class XXXX’ used without template parameters

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    This works for me. Is this closer to what you want?
    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    template <class MyDummyClass, typename ReturnType, typename Parameter>
    class SingularCallBack
    {
    public:
    	typedef ReturnType (MyDummyClass ::*Method)(Parameter);
    
    	SingularCallBack(MyDummyClass* _class_instance, Method _method)
    	{
    		class_instance = _class_instance;
    		method             = _method;
    	};
    
    	ReturnType execute(Parameter parameter)
    	{
    		return (class_instance->*method)(parameter);
    	};
    
    private:
    	MyDummyClass*   class_instance;
    	Method  method;
    };
    
    
    template <typename MyDummyClass, typename ReturnType, typename Parameter>
    class BaseClass
    {
    public:
    	std::vector<SingularCallBack<MyDummyClass, ReturnType, Parameter>> myvec;
    };
    
    template <typename ReturnType, typename Parameter>
    class AClass : public BaseClass<AClass<ReturnType, Parameter>, ReturnType, Parameter>
    {
    public:
    
    };
    
    int main()
    {     
    
    	return 0;
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115
    Thanks for all the effort, I tried to call the vector in the derived class, I failed
    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    template <class MyDummyClass, typename ReturnType, typename Parameter>
    class SingularCallBack
    {
    public:
    	typedef ReturnType (MyDummyClass ::*Method)(Parameter);
    
    	SingularCallBack(MyDummyClass* _class_instance, Method _method)
    	{
    		class_instance = _class_instance;
    		method             = _method;
    	};
    
    	ReturnType execute(Parameter parameter)
    	{
    		return (class_instance->*method)(parameter);
    	};
    
    private:
    	MyDummyClass*   class_instance;
    	Method  method;
    };
    
    
    template <typename MyDummyClass, typename ReturnType, typename Parameter>
    class BaseClass
    {
    public:
    	std::vector<SingularCallBack<MyDummyClass, ReturnType, Parameter> > myvec;
    };
    
    template <typename ReturnType, typename Parameter>
    class AClass : public BaseClass<AClass<ReturnType, Parameter>, ReturnType, Parameter>
    {
    public:     
         AClass ()
         {
    	  myvec.push_back (SingularCallBack < AClass, bool, std::string > (this, f, "sads"));
         }
         void f () 
         {
    	  cout << "\nsdfsdfdsf\n";
         }
    };
    
    int main()
    {     
    	return 0;
    }
    Error:
    Code:
    anisha@linux-uitj:~> g++ callback4.cpp
    callback4.cpp: In constructor ‘AClass<ReturnType, Parameter>::AClass()’:
    callback4.cpp:41: error: ‘myvec’ was not declared in this scope
    anisha@linux-uitj:~>

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    Templates are bastards.
    You may try this:
    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    using namespace std;
    
    template <class MyDummyClass, typename ReturnType, typename Parameter>
    class SingularCallBack
    {
    public:
    	typedef ReturnType (MyDummyClass ::*Method)(const Parameter&);
    
    	SingularCallBack(MyDummyClass* _class_instance, Method _method)
    	{
    		class_instance = _class_instance;
    		method             = _method;
    	};
    
    	ReturnType execute(Parameter parameter)
    	{
    		return (class_instance->*method)(parameter);
    	};
    
    private:
    	MyDummyClass*   class_instance;
    	Method  method;
    };
    
    
    template <typename MyDummyClass, typename ReturnType, typename Parameter>
    class BaseClass
    {
    public:
    	std::vector<SingularCallBack<MyDummyClass, ReturnType, Parameter> > myvec;
    };
    
    template <typename ReturnType, typename Parameter>
    class AClass : public BaseClass<AClass<ReturnType, Parameter>, ReturnType, Parameter>
    {
    public:     
    	AClass ()
    	{
    		this->myvec.push_back( SingularCallBack<AClass, ReturnType, Parameter>(this, &AClass::f) );
    	}
    	ReturnType f (const Parameter& foo) 
    	{
    		cout << foo;
    		return ReturnType();
    	}
    };
    
    int main()
    {     
    	AClass<int, int> test;
    	return 0;
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    There is some problem with your class SingularCallBack....
    Look over that..... I'll look over that too but now i got to go....

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment issues
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 01-13-2010, 11:55 AM
  2. Specialising a member function with a template template parameter
    By the4thamigo_uk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2007, 04:37 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 11:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21