user3407254
user3407254

Reputation: 55

C++ error: "no instance of function template matches the required type"

I'm a C++ newbie and am writing a class to add, subtract and multiply polynomials. I'm seeing this error:

"no instance of function template matches the required type"

resulting from these statements:

display(add, count);
display(sub, count);
display(mult, count);
P1.display(add, count); (as well as for sub and mult)
P2.display(add, count); (as well as for sub and mult)

My code is:

#include "stdafx.h"
#include <iostream>
#include <cstdlib>

using namespace std;

class Poly
{

private:

    int order;                                  // the order of the polynomial
    int *coeff;                                 // pointer to an array of coefficients
                                                // size of the coefficient array is predicated on [order + 1]
    int *add;
    int *sub;
    int *mult;

public:

//  Poly();                                     // the default constructor
    int setOrderAndCoeff();                     // sets the order and coefficients
    int display(int *data, int count);          // displays the resutling polynomial
    void addition(Poly P1, Poly P2);            // adds 2 polynomials
    void subtraction (Poly P1, Poly P2);        // subtracts 2 polynomials
    void multiplication (Poly P1, Poly P2);     // multiplies 2 polynomials

//  ~Poly();                                    // the destructor
};


int Poly::display(int *data, int count)
{
    for (int i = count; i >= 0; i--)
    {
        cout << data[i] << "x^" << i;
        if ((i - 1) != -1)
        {
            cout << "+";
        }
    }
    cout << "\n";
    return 0;
}

int Poly::setOrderAndCoeff()
{
    int i;
    cout << "Please enter the order of the polynomial: ";
    cin >> order;
    coeff = new int[order + 1];
    for (i = order; i >= 0; i--)
    {
        cout << "Please enter the coefficient of x^" << i << " :";
        cin >> coeff[i];
    }
    return 0;
}

void Poly::addition(Poly P1, Poly P2)
{
    int max;
    int i;
    max = (P1.order > P2.order) ? P1.order : P2.order;
    add = new int [max + 1];

    if (P1.order == P2.order)
    {
        for (i = P1.order; i >= 0; i--)
        {
            add[i] = P1.coeff[i] + P2.coeff[i];
        }
    }

    if (P1.order > P2.order)
    {
        for (i = P1.order; i > P2.order; i--)
        {
            add[i] = P1.coeff[i];
        }
        for (i = P2.order; i >= 0; i--)
        {
            add[i] = P1.coeff[i] + P2.coeff[i];
        }
    }

    if (P1.order < P2.order)
    {
        for (i = P2.order; i > P1.order; i--)
        {
            add[i] = P2.coeff[i];
        }
        for (i = P1.order; i >= 0; i--)
        {
            add[i] = P1.coeff[i] + P2.coeff[i];
        }
    }
    cout << "\nAddition:";
    display(add, count);
    cout << "\n";
}

void Poly::subtraction(Poly P1, Poly P2)
{
    int max;
    int i;
    max = (P1.order > P2.order) ? P1.order : P2.order;
    int *sub = new int[max + 1];

    if (P1.order == P2.order)
    {
        for (i = P1.order; i >= 0; i--)
        {
            sub[i] = P1.coeff[i] - P2.coeff[i];
        }
    }

    if (P1.order > P2.order)
    {
        for (i = P1.order; i > P2.order; i--)
        {
            sub[i] = P1.coeff[i];
        }
        for (i = P2.order; i >= 0; i--)
        {
            sub[i] = P1.coeff[i] - P2.coeff[i];
        }
    }

    if (P1.order < P2.order)
    {
        for (i = P2.order; i > P1.order; i--)
        {
            sub[i] = -P2.coeff[i];
        }
        for (i = P1.order; i >= 0; i--)
        {
            sub[i] = P1.coeff[i] - P2.coeff[i];
        }
    }
    cout << "\nSubtraction:";
    display(sub, count);
    cout << "\n";
}

void Poly::multiplication(Poly P1, Poly P2)
{
    int i;
    int j;
    int max;

    max = P1.order + P2.order;
    int *mult = new int[max + 1];

    for (i = P1.order; i >= 0; i--)
    for (j = P2.order; j >= 0; j--)
    {
        mult[i + j] += P1.coeff[i] * P2.coeff[i];
    }
        cout << "\nMultiplication:";
        display(mult, count);
}

int main()
{
    int choice;
    Poly P1, P2, P3;
    cout << "-------- Instructions --------" << endl;

    cout << "For polynomial 1... " << endl;
    P1.setOrderAndCoeff();

    cout << endl;

    cout << "For polynomial 2... " << endl;
    P2.setOrderAndCoeff();

    while (1)
    {
        cout << "\n******** Menu Selection ********" << endl;
        cout << "1: Addition\n2: Subtraction\n3: Mutiplication\n0: Exit" << endl;
        cout << "Please enter your choice (1, 2, 3 or 0):";
        cin >> choice;

        switch (choice)
        {
        case 1:
            cout << "\n-------- Addition --------\n";
            cout << "Polynomial 1: ";
            P1.display(add, count);
            cout << "Polynomial 2: ";
            P2.display(add, count);
            P3.addition(P1, P2);
            cout << "--------------------------\n";
            break;

        case 2:
            cout << "\n-------- Subtraction --------\n";
            cout << "Polynomial 1: ";
            P1.display(sub, count);
            cout << "Polynomial 2: ";
            P2.display(sub, count);
            P3.subtraction(P1, P2);
            cout << "--------------------------\n";
            break;

        case 3:
            cout << "\n-------- Multiplication --------\n";
            cout << "Polynomial 1: ";
            P1.display(mult, count);
            cout << "Polynomial 2: ";
            P2.display(mult, count);
            P3.multiplication(P1, P2);
            cout << "--------------------------\n";
            break;

        case 0:
            cout << "The program will now terminate.  Thank you." << endl;
            exit(0);

        default:
            cout << endl;
            cout << "You have entered an invalid selection." << endl;
            cout << "Please enter a positive integer between 0 and 3.";
            cout << endl;
        }
    }
    return 0;

Does anyone know what's causing this?

Thanks for any guidance you can give? -Ryan

Upvotes: 0

Views: 1503

Answers (3)

Vlad from Moscow
Vlad from Moscow

Reputation: 311126

In this code snippet

        switch (choice)
        {
        case 1:
            cout << "\n-------- Addition --------\n";
            cout << "Polynomial 1: ";
            P1.display(add, count);
            cout << "Polynomial 2: ";
            P2.display(add, count);
            P3.addition(P1, P2);
            cout << "--------------------------\n";
            break;

        case 2:
            cout << "\n-------- Subtraction --------\n";
            cout << "Polynomial 1: ";
            P1.display(sub, count);
            cout << "Polynomial 2: ";
            P2.display(sub, count);
            P3.subtraction(P1, P2);
            cout << "--------------------------\n";
            break;

        case 3:
            cout << "\n-------- Multiplication --------\n";
            cout << "Polynomial 1: ";
            P1.display(mult, count);
            cout << "Polynomial 2: ";
            P2.display(mult, count);
            P3.multiplication(P1, P2);
            cout << "--------------------------\n";
            break;

        case 0:
            cout << "The program will now terminate.  Thank you." << endl;
            exit(0);

        default:
            cout << endl;
            cout << "You have entered an invalid selection." << endl;
            cout << "Please enter a positive integer between 0 and 3.";
            cout << endl;
        }

in all calls of function display neither the first nor the second argument is defined. For example where is there defined add and count in main?

            P1.display(add, count);

The function is defined as having two parameters

int Poly::display(int *data, int count)

If you want to pass as the first argument for example data member add then you should write at least

            P1.display( P1.add, count);

making data member add public.

However I do not see where the second argument with name count is defined.

For example in member function void Poly::addition(Poly P1, Poly P2); count can be defined as an expression max + 1 But in the main you call display outside other member functions of the class. So I can conclude that the code is in general wrong.

Upvotes: 1

user3360398
user3360398

Reputation:

int setOrderAndCoeff()

the member method would return an integer. however, in your main function,

   int choice;
    Poly P1, P2, P3;
    cout << "-------- Instructions --------" << endl;

    cout << "For polynomial 1... " << endl;
    P1.setOrderAndCoeff();   '<=  this is valid as long as there exists a setOrderAndCoeff overloading, return type is void.

i think one way to fix the error would be changing

int setOrderAndCoeff()

to

void setOrderAndCoeff(){
    int i;
    cout << "Please enter the order of the polynomial: ";
    cin >> order;
    coeff = new int[order + 1];
    for (i = order; i >= 0; i--)
    {
        cout << "Please enter the coefficient of x^" << i << " :";
        cin >> coeff[i];
    }

}

similar changes are required to Display member method in the class.

Upvotes: 0

russw_uk
russw_uk

Reputation: 1267

It's not a great error message, certainly.

The issue is that when you call Poly::display() from main(), you're passing the names of the members of Poly - add, mult and count - which aren't visible from the main method as they're declared as private within the class.

To resolve this, you would need to either:

  1. Define a method which displays the members you need without having to pass a reference to them from outside the class (for example displayMult(), etc)
  2. move the main loop you currently have within main() somewhere where those fields are visible - either into a member of Poly or a friend function of Poly
  3. Add public getters to retrieve the values of those member variables to Poly

Upvotes: 0

Related Questions