Aquarius_Girl
Aquarius_Girl

Reputation: 22916

error: no matching function for call to

#include <iostream>
using namespace std;

template <typename x> x functionA (x, x);

int main ()
{
    functionA <double, double, double> (1, 1) << "\n";
}

template <typename x> x functionA (x arg1, x arg2)
{
    return arg1 + arg2;
}

This code results in:

error: no matching function for call to ‘functionA(int, int)’

What can be the reasons?

Upvotes: 1

Views: 1550

Answers (5)

iammilind
iammilind

Reputation: 69988

The line should be,

std::cout << functionA <double> (1, 1) << "\n";
^^^^^^^missing          ^^^^^^only 1 argument

Because, functionA takes only 1 template argument and thus you should call explicitly only with exactly one template argument.

The 3 arguments are needed in the case had there been your functionA was like,

template <typename x, typename y, typename z>
x functionA (y arg1, z arg2)
{
    return arg1 + arg2;
}

Upvotes: 1

Jagannath
Jagannath

Reputation: 4025

You don't need to specify the types multiple times. If your return type and the argument to the function is same, then you don't even need to specify the type. The below code compiles fine.

std::cout << functionA(1, 1) << std::endl;

Upvotes: 0

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361422

The function template has one template parameter only, and you're passing 3 template arguments to it:

functionA <double, double, double> (1, 1) << "\n";

Why 3 template arguments?

Just write:

functionA <double> (1, 1);

Or you can simply let the compiler deduce the template argument, as:

functionA(1.0, 1.0);  //template argument deduced as double!
functionA(1, 1);     //template argument deduced as int!

Upvotes: 1

Bj&#246;rn Pollex
Bj&#246;rn Pollex

Reputation: 76788

There are two things wrong here. First, you only need to specify one type for the template:

functionA<double>(1, 1)

Secondly, you are missing the std::cout at the beginning of that line.

Upvotes: 2

wilx
wilx

Reputation: 18228

This is wrong: functionA <double, double, double> (1, 1). You are trying to call the functionA() with three template parameters while your declaration of functionA has only 1 template parameter.

Beside that, the << "\n"; after the call does not make any sense either.

Upvotes: 1

Related Questions