Template Compile with g++ but not visual studio
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++?
10 Months Ago
Last Updated
Related Article: Opengl in visual studio (win32 app) help
is a solved C++ discussion thread by creative_m that has 2 replies, was last updated 1 year ago and has been tagged with the keywords: c++, opengl, visual-studio.
triumphost
Practically a Master Poster
630 posts since Oct 2009
Reputation Points: 59
Solved Threads: 56
Skill Endorsements: 1
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.
triumphost
Practically a Master Poster
630 posts since Oct 2009
Reputation Points: 59
Solved Threads: 56
Skill Endorsements: 1
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);.
mike_2000_17
21st Century Viking
3,166 posts since Jul 2010
Reputation Points: 2,082
Solved Threads: 636
Skill Endorsements: 42