简体   繁体   English

模板的MSVC重载错误,但不是g ++

[英]MSVC Overload error for templates but not g++

I've asked on other forums but no one has answered :SI keep getting: 我曾在其他论坛上提问,但没有人回答:SI不断得到:

None of the 6 overloads could convert all arguments. 6个重载都不能转换所有参数。

I only get this problem in visual studio though. 我只在Visual Studio中遇到此问题。 When I compile with g++ or codeblocks, it works perfectly fine. 当我使用g ++或代码块进行编译时,它可以很好地工作。

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])); 我一直这样称呼它: 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: 好吧,我不能说为什么会发生,但是MSVC似乎希望您定义一个指针变量作为引用传递:

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. 我的猜测是直接传递&Destination[I]仅给您指针值(元素的地址),而实际上却没有给您可以引用的指针变量。 GCC is perhaps more flexible in allowing you to do that with temporaries. 在允许您临时使用GCC时,GCC可能更灵活。

I don't know what the correct behaviour would be from a standards viewpoint. 从标准的角度来看,我不知道正确的行为是什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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