简体   繁体   中英

MSVC Overload error for templates but not g++

I've asked on other forums but no one has answered :SI 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:

typedef struct
{
    //......
} PanelItem;

std::vector<PanelItem> ListOfItems;

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 keep getting 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 ideas why? It compiles fine in codeblocks; just not visual studio.

I've been calling it like so: MemDeSerialize(ListOfItems, SerializedData, size_t(Data[2]));

Well I can't say why it happens but MSVC seems to want you to define a pointer variable to pass as the reference:

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* foo = &Destination[I];
        MemDeSerialize(foo, Source, Size);
    }
}

My guess is that passing &Destination[I] directly is only giving you the pointer value (address of the element), it doesn't actually give you a pointer variable that you can reference. GCC is perhaps more flexible in allowing you to do that with temporaries.

I don't know what the correct behaviour would be from a standards viewpoint.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM