gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:45:36 25 4
gpt4 key购买 nike

我是 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;

有人知道是什么原因造成的吗?

感谢您提供任何指导?-瑞安

最佳答案

在这段代码中

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

在函数显示的所有调用中,第一个和第二个参数都没有定义。例如main 中的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 但主要是您在类的其他成员函数之外调用 display。所以我可以得出结论,代码通常是错误的。

关于C++ 错误 : "no instance of function template matches the required type",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25068887/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com