Thread: template plus ambiguous overloading!!

  1. #1
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251

    template plus ambiguous overloading!!

    i get this errors
    call of overloaded ServeWrPar is ambiguous

    with the following code
    Code:
    int ServeWrPar(Type_StationArgs * StationArgs, unsigned int ParIndex, int Value, unsigned int ServerIndex, int sock)
    {
    	;//to be done
    }
    
    int ServeWrPar(Type_StationArgs * StationArgs, unsigned int ParIndex, unsigned int Value, unsigned int ServerIndex, int sock)
    {
    	int Result;
    	
    	switch	( ParIndex )
    			{
    			case 		MAX_PROCESSING_MSEC:		
    						Result = SetMaxProcessingMsec(StationArgs, ParIndex, Value, ServerIndex, sock);
    						break;	
    					
    		   
    			default:	Log(LogFile,"Parameter setting : request not available",1 );
    						Result = -1;
    						break;
    			}
    
     	return Result;
    }
    
    int ServeWrPar(Type_StationArgs * StationArgs,   unsigned int ParIndex, float Value, unsigned int ServerIndex, int sock)
    {
    	;//to be done
    }
    
    
    
    template < class Type_Value, class Type_WrParComm  > int HandleWrParFIFO( Type_Value DummyValue,  
    						Type_WrParComm * Comm,
    						pthread_mutex_t * Mutex, unsigned int * AttemptNumber, 
    						unsigned long int BlockingAttemptNumber, sem_t * CatastroficError, FILE * ErrLogFile, Type_StationArgs * Args )
    {
    
    
    	unsigned int ParIndex, TargetIndex, ServerIndex;
    	Type_Value Value;
    	int sock;
    	
    	int mutRes;
    	char LogString[512];
    	void * status;
    
    	
    	status=(void*)0;
    	mutRes=1;
    	*AttemptNumber++;
    	
    	//Critic region begin 
    	if ((*AttemptNumber)>BlockingAttemptNumber) mutRes=pthread_mutex_lock(Mutex);
    	else mutRes=pthread_mutex_trylock(Mutex);
    
    	
    		if (mutRes)
    		{	
    			if (mutRes==EBUSY)
    			{
    				return(ERR_OK);
    			}
    			else if (mutRes==EINVAL) 
        		{
        			sprintf(LogString," : mutex init error!\n");Log(LogFile,LogString,0 );
        			sem_post(CatastroficError);//CatastroficError=1;//to be tested outside by main thread
        			status=(void*)1;
        			pthread_exit(status);//to be studied
        		}
        		else 
        		{
        			sprintf(LogString," : mutex unkown error!\n");Log(LogFile,LogString,0 );
        			sem_post(CatastroficError);
        			status=(void*)1;
        			pthread_exit(status);//to be studied
        		}    			
    		} 
    		else
    		{
    		
    			if (Comm->ElementsNr)
    			{
    				Comm->ElementsNr-=1;
    
    				//N.B. once the requests were queued there was no more priority difference 
    				TargetIndex=Comm->TargetIndex[Comm->FirstIndex];
    				ParIndex=Comm->ParIndex[Comm->FirstIndex];
    				memcpy(Value,(char*)&(Comm->Value[Comm->FirstIndex]),sizeof(Value));
    				ServerIndex=Comm->ServerIndex[Comm->FirstIndex];
    				sock=Comm->sock[Comm->FirstIndex];				
    				
    				if (Comm->ElementsNr==0)	{Comm->FirstIndex=Comm->LastIndex=0;}
    				else
    				{
    					Comm->FirstIndex=(Comm->FirstIndex+1)&#37;WrFIFO_SIZE;					
    				}
    				
    			}
    			
    		
    			pthread_mutex_unlock(Mutex);
    			//critic region : end
    			*AttemptNumber=0;	
    			
    			if (!TargetIndex) ServeWrPar(Args, ParIndex, Value, ServerIndex, sock);//overloaded
    			else CAMParameterMailSet(Args, TargetIndex, ParIndex,Value, ServerIndex, sock);//overloaded
    		}
    
    	
    	return(ERR_OK);
    	
    }
    ideas to keep the template without having to make 3 copies of it??
    Last edited by mynickmynick; 08-26-2008 at 08:06 AM.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    And what is the type of Type_Value when the error occurs?

    Just guessing: it is possible that it is a double, in which case there is no suitable overload to call. (One solution: use the f suffix for float literals if the argument comes from a literal.)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    no the template is called 3 times with an int an unsigned int and a float respectively

    It seems the compiler does not resolve such an easy task

    It seems overloading nested inside templates is not supported

    While instead I showed in anothe thread that the opposite works : overloading of a function calling a template to have it available outside the module

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It seems the compiler does not resolve such an easy task

    It seems overloading nested inside templates is not supported
    I'm sure that the problem is in your code. By the way, you haven't shown how you call the function / instantiate the template.

    Here's a minimal example that demonstrates that it works (and a way to call the function that doesn't compile, with the similar error message that you get).

    Code:
    #include <iostream>
    
    void foo(int n)
    {
        std::cout << "int(" << n << ")\n";
    }
    
    void foo(unsigned int n)
    {
        std::cout << "unsigned int(" << n << ")\n";
    }
    
    void foo(float n)
    {
        std::cout << "float(" << n << ")\n";
    }
    
    template <class T>
    void bar(T n)
    {
        foo(n);
    }
    
    int main()
    {
        bar(42.0f);  //float(42)
        bar(42);  //int(42)
        bar(42u);  //unsigned int(42)
        //bar(42.0); //calls ambigous overload
    }
    And if the compiler can't deduce the type of the parameter correctly for some reason, you can always help it. E.g the error line may be forced to compile with
    Code:
        bar<float>(42.0);
    Last edited by anon; 08-26-2008 at 11:29 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Post the code that calls HandleWrParFIFO and/or the exact compiler error message. It should clear a lot up.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM