The error message "displays conflicting types for the parameter" const T "

What am I trying to do:

Write a specialized version of the template from the previous exercise for processing vector<const char*> and programs that use this specialization.

I wrote the program as follows:

 template<typename T> int count(vector<T> tvec, const T &t); template<> int count(vector<const char *> tvec, const char *const &s) { int count = 0; for (auto c : tvec) if (c == s) { ++count; } return count; } template<typename T> int count(vector<T> tvec, const T &t) { int count = 0; for (auto c : tvec) if (c == t) { ++count; } return count; } cout << count(svec, "GUO"); 

but i get an error

deduced conflicting types for parameter 'const T' ('std::basic_string<char>' and 'char [4]')

I want to know how to handle this. and besides, in the template function it seems that the array can be changed to a pointer, why can't my program process it?

+1
source share
2 answers

Do not output by both parameters, this leads to conflicts. Write this:

 template <typename T> int count(const vector<T>& tvec, const typename vector<T>::value_type& t); 

Also, consider overloading instead of specialization. Specializing a function template is pretty much not what you want.

+1
source

Firstly, it seems that svec is defined as vector<string> , perhaps it should be vector<const char*> ;

Second, explicitly define var as const char *;

Try the following:

 vector<const char*> svec; const char* chars = "GUO"; std::cout<<my_count(svec,chars); 

BTW: a variable of type char array (char []) can be used as a pointer of type char (char *), but they differ as a type, and they differ as a paremeter template.

0
source

Source: https://habr.com/ru/post/971895/


All Articles