Joao Pincho
Joao Pincho

Reputation: 979

c++ template operator no match found

Ok, new question about c++ templates...

I'm writing a template class for 2d/3d/4d vectors ( as in geometric vectors, not arrays ). All's well, after a bunch of questions here in SO, but now operators are not found, for some reason. If i declare them inside the class, it's ok, but if i declare them externally AS A TEMPLATE, they're not found. Funny enough, if i specifically declare them with the correct variable types, then all is well again. So it basically seems as though that function template is never instantiated.

So, the error is:

error: no match for ‘operator-’ (operand types are ‘Math::TVector<int, 3ul>’ and ‘Math::TVector<int, 3ul>’)

even though there's a function for it:

template <typename Type, unsigned TemplateElementCount>
Math::TVector <Type,TemplateElementCount> operator - ( Math::TVector <Type,TemplateElementCount> &First, Math::TVector <Type,TemplateElementCount> &Second )
{
Math::TVector <Type,TemplateElementCount> Result;
for ( unsigned cont = 0; cont < TemplateElementCount; ++cont )
    Result.Data[cont] = First.Data[cont] - Second.Data[cont];
return Result;
}

An example of the code is available at http://goo.gl/qrZaU1 I've tried declaring it inside the namespace, outside it, outside it with full resolution ( including Math:: everywhere ) and nothing works.. Can anyone lend me a hand? Thanks

EDIT: Full compile error is

main.cpp: In function 'int main(int, char**)':                                                                                  
main.cpp:16:23: error: no match for 'operator-' (operand types are 'Math::TVector<int, 3ul>' and 'Math::TVector<int, 3ul>')     
 Vector1 = Vector1 - Vector2;                                                                                               
                   ^                                                                                                        
main.cpp:16:23: note: candidate is:                                                                                             
In file included from main.cpp:2:0:                                                                                             
Point.h:171:43: note: template<class Type, unsigned int TemplateElementCount> Math::TVector<Type, TemplateElementCount> operator
-(Math::TVector<Type, TemplateElementCount>&, Math::TVector<Type, TemplateElementCount>&)                                       
 Math::TVector <Type,TemplateElementCount> operator - ( Math::TVector <Type,TemplateElementCount> &First, Math::TVector <Type,Te
mplateElementCount> &Second )                                                                                                   
                                       ^                                                                                    
Point.h:171:43: note:   template argument deduction/substitution failed:                                                        
main.cpp:16:25: note:   mismatched types 'unsigned int' and '#'integer_cst' not supported by dump_type#<type error>'            
 Vector1 = Vector1 - Vector2;                                                                                               
                     ^                                                                                                      
main.cpp:16:25: note:   'Math::TVector<int, 3ul>' is not derived from 'Math::TVector<Type, TemplateElementCount>' 

Upvotes: 1

Views: 411

Answers (1)

Andy Prowl
Andy Prowl

Reputation: 126562

The problem (or at least, one of the problems) seems to be that you are using unsigned as the type of the second non-type template argument of operator -, while the class TVector is instantiated with a corresponding non-type template argument of type of std::size_t. The two types are not necessarily the same (according to the compiler error you are receiving, it seems std::size_t resolves to unsigned long on your platform), hence the error.

Changing the function's signature as follows should fix the problem:

template <typename Type, std::size_t TemplateElementCount>
//                       ^^^^^^^^^^^
Math::TVector <Type,TemplateElementCount> operator - (
    Math::TVector <Type,TemplateElementCount> &First, 
    Math::TVector <Type,TemplateElementCount> &Second )
{
    // ...
}

Upvotes: 2

Related Questions