Thread: Compiler not finding my function

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    2

    Compiler not finding my function

    Hello All

    Strange problem, I just do not understand what is going on... basically, I have a series of template functions and template classes. I am trying to use the template functions within template classes to help me with some of the basic nitty gritty stuff like copying vectors.

    I have a template class called state, the real version has many more members, but the one here has a vector<T> member. I am trying to write an assignment operator that can copy the from another state with a different template parameter. This assignment operator calls a template<class T,class U> copyVector function. Yet, when I compile it seems that the compiler cannot find the copyVector function. I don't know what I need to do to get this to bind.

    Here is some sample code:

    Code:
    #include <vector>
    
    template<class T,class U>
    void copyVector(std::vector<T, std::allocator<T> > &vct, std::vector<U, std::allocator<U> > &asg)
    {
            vct.resize(asg.size());
            for(unsigned long i;i<asg.size();i++)
                    vct[i]=asg[i];
            return;
    }
    
    template<class T>
    struct state
    {
            std::vector<T> data;
    
            template<class U> state<T>& operator=(const state<U> &asg)
            {
                    copyVector<T,U>(this->data,asg.data);
                    return *this;
            }
    };
    
    struct cont
    {
            double value;
            cont& operator=(const cont &asg)
            {
                    this->value=asg.value;
                    return *this;
            }
    };
    
    int main()
    {
            state<double> base;
            state<cont> copy;
    
            copy=base;
    }
    These are the error messages I get:

    Code:
    test.cpp: In member function ‘state<T>& state<T>::operator=(const state<U>&) [with U = double, T = cont]’:
    test.cpp:35:   instantiated from here
    test.cpp:20: error: no matching function for call to ‘copyVector(std::vector<cont, std::allocator<cont> >&, const std::vector<double, std::allocator<double> >&)’

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The compiler is trying to find a match with a const second argument. Making the second argument const allows you to move forward, where you get a new error (specifically you try to copy a double to a cont, but you don't have a operator= that does that).

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    asg in your assignment operator is const, so asg.data is also (as a consequence) const. The second argument of your copyVector() therefore needs to be const.

    Your next problem will be that there is no way of converting a cont to a double, which I assume is self-explanatory.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    2
    Uuuuuhhhhhh

    Ok light bulb on!!!!

    const-correctness

    gets me every time

    With all the crazy templates every where I though it had something to do with that. Name binding in templates does not always give expected results. Yet that is another post.

    Thanks guys, I have every thing working. Even have all the correct const and operator= it all compiles past those errors. On to the next set....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  2. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  3. Replies: 5
    Last Post: 02-08-2003, 07:42 PM
  4. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM