[Erledigt] template fehler?



  • brotbernd schrieb:

    ämmm... kannst du als id nicht einfach die Position in einem vector nehmen?
    Die ist eindeutig und sortiert. Diese ganzen maps kommen mir sowieso grad seltsam vor.

    Position als id ist keine gute Idee.
    Eine id darf nicht nach einem Löschvorgang noch existieren.



  • Entschuldigt dass ich euch so foltere aber irgendwie scheinen templates nicht meine Stärke zu sein und die Kompilierfehler sind noch kryptischer.

    fwd_type soll den benötigten Typ weiterleiten.

    #include <map>
    
    using namespace std;
    
    class Utm{ };
    class Knot{ };
    
    template<typename> struct fwd_type{ };
    template<> struct fwd_type<std::map<int, Utm>> { typedef Utm class_type; };
    template<> struct fwd_type<std::map<int, Knot>> { typedef Knot class_type; }; 
    
    template<typename T> struct topology{
    
    	typedef typename fwd_type<T>::class_type type_t;
    
    	typedef pair<int,type_t> pair;
    	typedef map<int,type_t>::iterator it;
    	typedef map<int,type_t>::const_iterator const_it;
    	typedef std::pair<it,bool> bool_it;
    
    };
    
    class Geotopology{
    
    private:
    	template<typename T, typename S> int get_id_by_pos(T& container, S pos) const;
    	map<int, Utm> coords;
    	map<int, Knot> knots;
    
    public:
    	int get_coord_by_pos(int pos_in_map) const;
    	int get_knot_by_pos(int pos_in_map) const;
    
    };
    
    template<typename T, typename S> int Geotopology::get_id_by_pos(T& container, S pos) const{
    
    	typedef typename topology<T>::const_it const_iter_t;
    
    	const_iter_t i = container.begin();
    	std::advance(i, pos-1);
    
    	return i->first;
    } 
    
    int Geotopology::get_coord_by_pos(int pos_in_map) const{ 
    
    	return get_id_by_pos(coords,pos_in_map); 
    
    }
    
    int main(void){ return 0; }
    

    Als Compileroutput kommt:

    1>(16): error C2327: 'topology<T>::type_t' : is not a type name, static, or enumerator
    1>(21) : see reference to class template instantiation 'topology<T>' being compiled
    1>(16): error C2065: 'type_t' : undeclared identifier
    1>(17): error C2327: 'topology<T>::type_t' : is not a type name, static, or enumerator
    1>(17): error C2065: 'type_t' : undeclared identifier
    1>(17): warning C4346: 'std::map<_Kty,_Ty,_Pr,_Alloc>::iterator' : dependent name is not a type
    1>          prefix with 'typename' to indicate a type
    1>(17): error C2146: syntax error : missing ';' before identifier 'it'
    1>(17): error C2838: 'iterator' : illegal qualified name in member declaration
    1>(17): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>(18): error C2327: 'topology<T>::type_t' : is not a type name, static, or enumerator
    1>(18): error C2065: 'type_t' : undeclared identifier
    1>(19): error C2065: 'it' : undeclared identifier
    

    Warum bekomme ich den richtigen Typ nicht weitergeleitet, es kann doch nicht so schwer sein. Woraus soll ich im Compileroutput schauen?



  • typename fwd_type<T>::class_type type_t;
    

    da fehlt nen typedef 😉

    bb



  • unskilled schrieb:

    typename fwd_type<T>::class_type type_t;
    

    da fehlt nen typedef 😉

    bb

    Danke, das war wohl der erste von mehreren Fehlerserien 🙂

    1>(17): warning C4346: 'std::map<int,fwd_type<<unnamed-symbol>>::class_type>::iterator' : dependent name is not a type
    1>          prefix with 'typename' to indicate a type
    1>(21) : see reference to class template instantiation 'topology<T>' being compiled
    1>(17): error C2146: syntax error : missing ';' before identifier 'it'
    1>(17): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>(18): warning C4346: 'std::map<int,fwd_type<<unnamed-symbol>>::class_type>::const_iterator' : dependent name is not a type
    1>          prefix with 'typename' to indicate a type
    1>(18): error C2146: syntax error : missing ';' before identifier 'const_it'
    1>(18): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>(19): error C2065: 'it' : undeclared identifier
    


  • Man... ich habe das const in

    template<typename T, typename S> int Geotopology::get_id_by_pos(T& container, S pos) const
    

    übersehen.

    Natürlich gehört in fwd_type auch das const zur std::map<>

    template<> struct fwd_type<const std::map<int, Utm>> { typedef Utm class_type; };
    template<> struct fwd_type<const std::map<int, Knot>> { typedef Knot class_type; };
    

    Danke unskilled du hast mich vor der Verzweiflung bewahrt.


Anmelden zum Antworten