c++ - class template, with template function, specialization -


i'm trying write template class aids programmer in writing correct code. calculate route san francisco new york via washington, given route san francisco washington , route washington new york. when given route san francisco los angeles , washington new york, compiler report error.

here class (without normal workhorse function):

extern const char miami[] = "miami"; template< const char* finish, const char* start> class route { public: route(); ~route() {}; template<const char* otherplace> route<start, otherplace> alterfinish() const; }; template<const char* start> // specialisation template<const char* otherplace> // allow miami finish changed else. route<start, otherplace> route<start, miami>::alterfinish() const{ return route<start, otherplace>(); } 

unfortunately not work. 'invalid use of incomplete type' error. same sort of function without specialisation compile. doing wrong?

invalid use of incomplete type indicates use haven't declared yet. in case it's partial specialization of route:

template <const char* start> class route<start,miami>{ template<const char* otherplace> route<start, otherplace> alterfinish() const; }; 

why need this? well, want create method alterfinish(), should specialized template<class start> route<start, miami>. full name route<start, miami>::alterfinish(). you've never defined template<class start> route<start, miami> anywhere. it's incomplete type, compiler isn't able compile.

however, believe there other things wrong in program. necessary use templates? wouldn't std::map<std::pair<city,city>, double> sufficient costs? want save cities string literals?

also, it's not clear why program crashes in special scenario. there's not enough code @ all, , code provided won't provide compiler error unless 1 adds necessary tweaks , moves many symbols.


Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -