How do I use templating properly? I'm having a

2019-07-23 01:40发布

问题:

EDIT 11/3/2011 9:37pm:

This is my error:

 error: no matching function for call to "charList_join(const char [1], CISP430_A5::linked_list<char>&)"

and this is my prototype:

template <typename Item>
string charList_join(const char* glue, linked_list<char> pieces);

and this is my function call:

charList_join("", usedChars)

where usedChars is a static linked_list<char> declared in the same scope where charList_join() is called.

EDIT 11/4/2011 8:45am: Ok, so here is my code with unnecessary functions removed:

[sehe: edited from pastebin to github]

  • browse it online at github
  • download it with git:

     git clone git://gist.github.com/1340832.git
    

I'm getting the error on line 57, column 68 of premute_append.cpp. I've included the makefile so you can attempt to build it if you want. I'm only getting one single error at this point, but I just don't have any idea what it means.

If you try to compile the error will look like this:

[cisw320b_stu022@scc-bdiv-cis assn5]$ make
g++ -c main.cpp
g++ -c permute_append.cpp
permute_append.cpp: In member function âCISP430_A5::linked_list<std::basic_string<char> > CISP430_A5::permute_append::permute(CISP430_A5::linked_list<char>)â:
permute_append.cpp:57:68: error: no matching function for call to âcharList_join(const char [1], CISP430_A5::linked_list<char>&)â
make: *** [permute_append.o] Error 1

Any idea why I'm getting this error?

回答1:

string charList_join(const char* glue, linked_list<char> pieces);

is actually

template< typename Item >
string charList_join(const char* glue, linked_list<char> pieces);

Since Item is not detected from any of the arguments, you should pass it explicitly:

charList_join<SomeItemType>( "", usedChars);

Or perhaps you just wanted instead:

template< typename Item >
string charList_join(const char* glue, linked_list<Item> pieces);


回答2:

You said:

and this is my prototype:

string charList_join(const char* glue, linked_list<char> pieces);

But it is not. According to the code you provided, your declaration is:

template <typename Item>
string charList_join(const char* glue, linked_list<char> pieces);

Which means that you need to call it like so:

charList_join<char>("", usedChars)

However, you don't actually use Item within this function, so you should just remove the template specifier (from the declaration and the definition) and call it as you were. Better still, pass the list by const-reference:

string charList_join(const char* glue, const linked_list<char>& pieces);

The call remains:

charList_join("", usedChars)