C ++错误:"没有函数模板的实例匹配所需的类型"

时间:2014-07-31 21:01:59

标签: c++ class

我是一个C ++新手,正在编写一个类来添加,减去和乘法多项式。我看到了这个错误:

"没有函数模板的实例匹配所需的类型"

由这些陈述产生:

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)

我的代码是:

#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;

有谁知道造成这种情况的原因?

感谢您提供任何指导? -Ryan

3 个答案:

答案 0 :(得分:1)

在此代码段中

        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;
        }

在所有函数调用中都没有定义第一个和第二个参数。例如 主要在哪里定义了addcount

            P1.display(add, count);

该功能被定义为具有两个参数

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

如果你想作为示例数据成员add的第一个参数传递,那么你应该写至少

            P1.display( P1.add, count);

使数据成员公开。

但是我没有看到名称为count的第二个参数定义在哪里。

例如在成员函数void Poly :: addition(Poly P1,Poly P2); count可以定义为表达式max + 1但是在main中你可以在类的其他成员函数之外调用display。所以我可以得出结论,代码总体上是错误的。

答案 1 :(得分:0)

当然,这不是一个很好的错误信息。

问题在于,当您从Poly::display()致电main()时,您传递的是Poly - addmult成员的姓名}和count - 这些在主要方法中是不可见的,因为它们在类中被声明为私有。

要解决此问题,您需要:

  1. 定义一个方法,显示您需要的成员,而不必从类外传递对它们的引用(例如displayMult()等)
  2. 将您当前拥有的主循环移动到main()这些字段可见的某个位置 - 或者成为Poly的成员或friend Poly函数
  3. 添加公共getter以将这些成员变量的值检索到Poly

答案 2 :(得分:0)

int setOrderAndCoeff()

成员方法将返回一个整数。但是,在你的主要功能中,

   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.

我认为解决错误的一种方法是改变

int setOrderAndCoeff()

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];
    }

}

类中显示成员方法需要进行类似的更改。