Templates mit mehreren Typen



  • Hallo,

    ich habe folgendes Template definiert:

    template<template<class B, class C> class A, class B, class C>
    void function(std::string a) {
    	// create
    	boost::shared_ptr<A<B, C> > approach(new A<B, C>(a));
    	std::string type = C::getType();
    
    	// print out
    	std::cout << "Type: " << type << std::endl;
    	std::cout << "Name: " << approach->getName() << std::endl;
    }
    

    Wenn ich das ganze benutzen möchte:

    std::string str = "hallo";
    function<E,F,G>(str);
    

    erhalte ich einen "no matching function for call to"-Fehler. Kann mir jemand dabei weiterhelfen?

    Grüße,
    mikemodanoxxx



  • Schön, dass du dir die vollständige Fehlermeldung und ein übersetzbares Beispiel gespart hast. Ich spare mit die Hilfe ...



  • Der Fehler liegt woanders. Minimal ergänzt funktioniert der gezeigte Code einwandfrei:

    #include <iostream>
    #include <string>
    
    #include <boost/shared_ptr.hpp>
    
    template<template<class B, class C> class A, class B, class C>
    void function(std::string a) {
        // create
        boost::shared_ptr<A<B, C> > approach(new A<B, C>(a));
        std::string type = C::getType();
    
        // print out
        std::cout << "Type: " << type << std::endl;
        std::cout << "Name: " << approach->getName() << std::endl;
    }
    
    template<typename T, typename U>
    struct E { 
      E(std::string const &) { }
      std::string getName() { return "E"; }
    };
    
    struct F { };
    struct G { static std::string getType() { return "G"; } };
    
    int main() {
      std::string str = "hallo";
      function<E, F, G>(str);
    }
    

    gibt

    Type: G
    Name: E
    


  • Hallo,

    ja, danke. Das Problem war, dass B auch ein Template ist, was mit B<C> aufgerufen wird.

    So funktioniert es auch bei mir:

    template<template<template<class C> class B, class C> class A, template<class C> class B, class C>
    void function(std::string a);
    

Anmelden zum Antworten