Thread: Passing Template values on to other classes

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    7

    Passing Template values on to other classes

    Hi, I've got what seems like a simple enough question about templates. What i want to do, is take the type parameter (from the template) in one class and pass that directly to another class. Like this:

    Code:
    template <class T>
    class classone
    {
    public:
         classone();        //constructor
         ~classone();     //destructor
    
         somefunc(T * myvar)
         {
               classtwo<T> MyInstancec2(somevar);
         }
    }
    
    
    template <class X>
    class classtwo
    {
    public:
         classtwo(X mydat)      //constructor class 2
         {
              X * thedataofXtype = &mydat;    
             itsData = thedataofXtype;
         }
    private:
         X * itsData;
    }
    The compiler keeps telling me that "there is no appropriate constructor for classone [ X=(some type) ]" where (some type) is the type of variable I pass to T. Any help?

    BTW my compiler is VC.net 2003.

    All help is greatly appreciated

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Its hard to say because I think when you typed up the example you made some mistakes. It would be better if you could reproduce the problem with a small and compilable example and post that.

    That said, one thing to check is that you pass somevar into the constructor of class two. Assuming that's a typo and you meant myvar, then myvar is a pointer of type T but the constructor of class two takes an object of type T. Maybe you should either dereference the pointer before passing it or change your classtwo constructor to accept a pointer.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    7
    Well, "somevar" is really just any variable, it doesn't matter what the type is. But I guess what I really need to know is this:

    In my example, when I instantiate classone with some type passed to it through the template say, an int, like this:

    Code:
    classone<int> MyInstanceofClassOne;
    is the int type going to get correctly passed into class two so that class two will be able to use int as well? Because the way I have it written it seems like it should work, but it isn't. Thanks for the quick reply.

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    It works for me in this code example, so the problem is probably in your code.
    Code:
    #include <iostream>
    
    template <class T>
    class classone
    {
    public:
        classone() { }
        ~classone() { }
    
        void somefunc(T myvar)
        {
            classtwo<T> MyInstancec2(myvar);
            MyInstancec2.displayData();
        }
    };
    
    
    template <class X>
    class classtwo
    {
    public:
        classtwo(X mydat) : itsData(mydat) { }
    
        void displayData() { std::cout << itsData << std::endl; }
    
    private:
        X itsData;
    };
    
    int main()
    {
        classone<int> myClassOne;
        myClassOne.somefunc(3);
    }

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >>"somevar" is really just any variable, it doesn't matter what the type is
    What jlou and your compiler are telling you is that it does matter. The constructor for classtwo<T> takes type "T", not "T*", or anything else.

    >> is the int type going to get correctly passed into class two...?
    When done correctly, yes.

    >>...it seems like it should work, but it isn't.
    >>>>It would be better if you could reproduce the problem with a small and compilable example and post that.

    gg

  6. #6
    Registered User
    Join Date
    Oct 2004
    Posts
    7
    ok, thanks jlou, that works for me too so it shows me that my idea wasn't wrong. That's basically what i wanted to know.

    codeplug: about the first point, i forgot that i put X as one of the parameters in class two, but the reason i said it didn't matter was because that wasn't really what my question was. But I think now that i know it does work(wasn't sure before, so i didn't want to look for a bug in my code if it wasn't even possible), i will just have to find my bug. Thanks for the help everyone!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Replies: 6
    Last Post: 12-06-2005, 09:23 AM
  3. Template specialization
    By CornedBee in forum C++ Programming
    Replies: 2
    Last Post: 11-25-2003, 02:02 AM
  4. Operator overloading in template classes
    By moejams in forum C++ Programming
    Replies: 5
    Last Post: 07-21-2003, 05:16 PM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM