We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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++?

2
Contributors
2
Replies
17 Hours
Discussion Span
10 Months Ago
Last Updated
3
Views
Question
Answered
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
Question Self-Answered as of 10 Months Ago

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
Moderator
3,166 posts since Jul 2010
Reputation Points: 2,082
Solved Threads: 636
Skill Endorsements: 42

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.0586 seconds using 2.68MB