I keep getting "None of the 6 overloads could convert all arguments". I only get this problem in visual studio though. When I compile with g++ or codeblocks, it works perfectly fine.

The code I'm calling my templates with is: MemDeSerialize(ListOfItems, SerializedData, size_t(Data[2]));

The definitions:

template<typename T>
void MemDeSerialize(T& Destination, unsigned char* &Source){...}

template<typename T>
void MemDeSerialize(T*& Destination, unsigned char* &Source, size_t Size){...}

template<typename T>
void MemDeSerialize(std::vector<T> &Destination, unsigned char* &Source, size_t Size)
{
    Destination.resize(Size);
    for (size_t I = 0; I < Size; ++I)
        MemDeSerialize(&Destination[I], Source, Size);
}


                                                /** FUNCTION OVERLOADS **/


void MemDeSerialize(Model* Destination, unsigned char* &Source);

void MemDeSerialize(PanelItem* Destination, unsigned char* &Source);

void MemDeSerialize(Compass* Destination, unsigned char* &Source);

void MemDeSerialize(FontChar* Destination, unsigned char* &Source);

The Error I get is:

1>error C2665: 'MemDeSerialize' : none of the 6 overloads could convert all the argument types
1>could be 'void MemDeSerialize<PanelItem>(T *&,unsigned char *&,size_t)'
1>          with
1>          [
1>              T=PanelItem
1>          ]
1>          while trying to match the argument list '(PanelItem *, unsigned char *, size_t)'
1>          see reference to function template instantiation 'void MemDeSerialize<PanelItem>(std::vector<_Ty> &,unsigned char *&,size_t)' being compiled
1>          with
1>          [
1>              _Ty=PanelItem
1>          ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Any idea why it's doing that in visual studio but not g++?

Solved.. For anyone with this problem, the solution was:

template<typename T>
void MemDeSerialize(std::vector<T> &Destination, unsigned char* &Source, size_t Size)
{
    Destination.resize(Size);
    for (size_t I = 0; I < Size; ++I)
    {
        T* Object = &Destination[I];             //Using a temporary object fixes it.
        MemDeSerialize(Object, Source, Size);
    }
}

It's to use a temporary object.

The problem is that your function overload:

template<typename T>
void MemDeSerialize(T*& Destination, unsigned char* &Source, size_t Size){...}

takes a reference to a pointer. And because you call it with &Destination[I], which creates a pointer to the Ith element, that pointer is a temporary object, and it cannot be bound to a reference (only lvalues can bind to non-const references). In this case, there is no reason to take the pointer by reference, so, you can fix your problem with this:

template<typename T>
void MemDeSerialize(T* Destination, unsigned char* &Source, size_t Size){...}

Notice the lack of the & symbol. This should allow you to call the function with MemDeSerialize(&Destination[I], Source, 1);.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.