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

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

本文关键字:类型 函数模板 实例 错误 C++      更新时间:2023-10-16

我是一个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: Additionn2: Subtractionn3: Mutiplicationn0: 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

在这个代码片段中

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

在函数display的所有调用中,既没有定义第一个参数,也没有定义第二个参数。例如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。因此,我可以得出结论,该代码总体上是错误的。

这当然不是一个好的错误消息。

问题是,当您从main()调用Poly::display()时,您传递的是Polyaddmultcount的成员的名称,这些名称在主方法中不可见,因为它们在类中被声明为private。

要解决此问题,您需要:

  1. 定义一个方法,该方法显示所需的成员,而不必从类外传递对它们的引用(例如displayMult()等(
  2. 将当前在main()中的主循环移动到那些字段可见的位置,或者移动到Poly的成员中,或者移动Polyfriend函数中
  3. 添加公共getter以检索Poly中那些成员变量的值
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];
    }
}

需要对类中的Display成员方法进行类似的更改。