2

I'm getting the following compiler error

error: no matching function for call to 'infxTree(std::string&)'

For this bit of code.

int main(){
string infxStr;

cout << "Enter an infix string: " << endl;
cin >> infxStr;


prefixOutput(infxTree(infxStr));
postorderOutput(infxTree(infxStr), ' ');
displayTree(infxTree(infxStr), infxStr.size());
    return 0;

}

I get the error on all the last 3 lines. Here's the function:

template <typename T>
tnode<T> infxTree(const string& iexp);

Any ideas what I'm doing wrong? Thanks!

4

2 回答 2

4

You have to give the template parameter explicitly:

infxTree<Foo>(infxStr)

Where Foo is the class type provided to your templated tnode class.

于 2013-05-19T22:06:43.253 回答
3

Since there is no clue in the function signature as to what T is, you have to specify it explicitly as a template type parameter.

inxTree<int>(infxStr);

This can be omitted if you have any arguments that depend on T, that the compiler can use to infer the type:

node<T> inxTree(string str, T item) { /* ... */ }
int item;
inxTree(infxStr, item); // OK
于 2013-05-19T22:09:03.237 回答