Portál AbcLinuxu, 14. května 2025 04:00
using std::max; typedef double Real; // obezlička na template alias (ano, vím že je v c++0x, ale gcc neumí) template<typename Scalar> struct Vector3{ typedef Matrix<Scalar,3,1> type; }; typedef Vector3<Real>::type Vector3r; // funkce // argumenty byly původně const, ale nemá to vliv na výsledek template<typename Scalar> typename Vector3<Scalar>::type componentMaxVector(typename Vector3<Scalar>::type& a, typename Vector3<Scalar>::type& b){ return typename Vector3<Scalar>::type(max(a.x(),b.x()),max(a.y(),b.y()),max(a.z(),b.z()));}Normálně to funguje, až na to, že v kódu:
Vector3r a,b,c; // přiřazení do a,b c=componentMaxVector(a,b);kde by se měla podle mě zavolat šablonová funkce se
Scalar=Real
(tedy Vector3<Scalar>::type=Vector3r
) mi g++ (4.4 i 4.5) nadává:
error: no matching function for call to 'componentMaxVector(Vector3r&, Vector3r&)'Dokáže někdo vysvětlit, proč g++ tu šablonu nevidí? Díky.
Řešení dotazu:
Kdyz mate funkci deklarovanou takhle, tak vam c++ argumenty nevydedukuje. Ale takhle to pujde:
template<typename Scalar> Matrix<Scalar, 3, 1> componentMaxVector(const Matrix<Scalar, 3, 1>& a, const Matrix<Scalar, 3, 1>& b) { return typename Vector3<Scalar>::type(max(a.x(),b.x()), max(a.y(),b.y()), max(a.z(),b.z())); }
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.