C ++错误:'没有从[类名称]到“int”的适当转换'' [英] C++ error: 'No suitable conversion from [class name] to "int" '
问题描述
我正在处理一个类,用于多项式的加,减和乘法。
我遇到以下错误:
'没有合适的转换函数从[class name] int'
在语句中:
(加法函数)
return deriv;
任何人都可以提供有关如何纠正这些问题的指导?
非常感谢提前,
Ryan
这是我的代码:
#includestdafx.h
#include< iostream>
using namespace std;
class Poly
{
private:
// int ord; //多项式的阶数
// int coeff [100];
public:
int ord; //多项式的阶数
int coeff [100];
int a,b,c;
Poly(); // constructor
int addition(int); //添加2个多项式
int subtraction(int); // subtracts 2 polynomials
int multiplication(int); //将2个多项式相乘
void evaluate(int); //使用Horner的方法来计算并返回在x
处求值的多项式int differentiate(int); //
void set(int,int); // mutator function
int order();
void print(); //打印结果
};
Poly :: Poly()//默认构造函数
{
for(int i = 0; i <100; i ++)
{
coeff [i] = 0;
}
}
void Poly :: set(int a,int b)// mutator function
{
// coeff = new Poly [ b + 1]。
coeff [b] = a;
ord = order();
}
int Poly :: order()
{
int d = 0;
for(int i = 0; i <100; i ++)
if(coeff [i]!= 0)d =
return d;
}
void print()
{
int coeff [] = {0};
for(int i = 99; i> = 0; i--)
{
if(coeff [i]!= 0)
{
cout<< coeff [i]< x ^<< i<< ;
}
}
}
int evaluate(int x)
{
int p = 0;
for(int i = ord; i> = 0; i--)
p = coeff [i] +(x * p)
return p;
}
int Poly :: differentiate(int)
{
if(ord == 0)
{
Poly t;
t.set(0,0);
return t;
}
聚衍生;
deriv.ord = ord - 1;
for(int i = 0; i deriv.coeff [i] =(i + 1)* coeff [i + 1]
return deriv;
}
int Poly :: addition(Poly b)
{
Poly a = * this;
Poly c;
for(int i = 0; i <= a.ord; i ++)
c.coeff [i] + = a.coeff [i]
for(int i = 0; i <= b.ord; i ++)
c.coeff [i] + = b.coeff [i]
c.ord = c.order();
return c;
}
多边形减法(Poly b)
{
Poly a = * this;
Poly c;
for(int i = 0; i <= a.ord; i ++)
c.coeff [i] + = a.coeff [i]
for(int i = 0; i <= b.ord; i ++)
c.coeff [i] - = b.coeff [i]
c.ord = c.order();
return c;
}
int Poly ::乘法(Poly b)
{
Poly a = * this;
Poly c;
for(int i = 0; i <= a.ord; i ++)
for(int j = 0; j <= b.ord; j ++)
c.coeff [i + j] + =(a.coeff [i] * b.coeff [j]);
c.order = c.order();
}
int main()
{
Poly a,b,c,d;
a.set(7,4); // 7x ^ 4
a.set(1,2); // x ^ 2
b.set(6,3); // 6x ^ 3
b.set(-3,2); // -3x ^ 2
c = a.subtraction(b); //(7x ^ 4 + x ^ 2) - (6x ^ 3 - 3x ^ 2)
c.print();
cout<< \\\
;
d = c.differentiate()。differentiate();
d.print();
cout<< \\\
;
cout<< (2); // replace 2 with 2
cin.get();
return 0;
}
在此函数中:
int Poly :: addition(Poly b)
{
Poly a =这个;
Poly c;
for(int i = 0; i <= a.ord; i ++)
c.coeff [i] + = a.coeff [i]
for(int i = 0; i <= b.ord; i ++)
c.coeff [i] + = b.coeff [i]
c.ord = c.order();
return c;
}
你说你会返回一个int:
int Poly :: addition(Poly b)
// ^ ------承诺返回int
但您返回 c
其中 c
是 Poly
。更改返回类型:
聚Poly :: addition(Poly b)
int Poly :: differentiate(int)
有同样的问题。
将其更改为
Poly Poly :: differentiate(int)
BTW - 为什么会忽略它
int
?
此外,
int Poly :: multiplication(Poly b)
不返回任何东西。
您还有其他问题。例如,print和evaluate函数应该是成员函数,使用std :: vector存储系数可能更有意义。
编译器可能告诉你如果你仔细看看错误的行号。
I'm working on a class for the addition, subtraction and multiplication of polynomials.
I'm getting the following errors:
'No suitable conversion function from [class name] to "int" '
in statements:
return c; (addition function) return deriv;
Can anyone offer any guidance on how to correct these?
Thanks very much in advance, Ryan
Here is my code:
#include "stdafx.h" #include <iostream> using namespace std; class Poly { private: // int ord; // the order of the polynomial // int coeff[100]; public: int ord; // the order of the polynomial int coeff[100]; int a, b, c; Poly(); // constructor int addition(int); // adds 2 polynomials int subtraction(int); // subtracts 2 polynomials int multiplication(int); // multiplies 2 polynomials void evaluate(int); // uses Horner's method to compute and return the polynomial evaluated at x int differentiate(int); // void set(int, int); // mutator function int order(); void print(); // prints the results }; Poly::Poly() // the default constructor { for (int i = 0; i < 100; i++) { coeff[i] = 0; } } void Poly::set(int a, int b) // mutator function { // coeff = new Poly[b + 1]; coeff[b] = a; ord = order(); } int Poly::order() { int d = 0; for (int i = 0; i < 100; i++) if (coeff[i] != 0) d = i; return d; } void print() { int coeff[] = { 0 }; for (int i = 99; i >= 0; i--) { if (coeff[i] != 0) { cout << coeff[i] << "x^" << i << " "; } } } int evaluate(int x) { int p = 0; for (int i = ord; i >= 0; i--) p = coeff[i] + (x * p); return p; } int Poly::differentiate(int) { if (ord == 0) { Poly t; t.set(0, 0); return t; } Poly deriv; deriv.ord = ord - 1; for (int i = 0; i < ord; i++) deriv.coeff[i] = (i + 1) * coeff[i + 1]; return deriv; } int Poly::addition(Poly b) { Poly a = *this; Poly c; for (int i = 0; i <= a.ord; i++) c.coeff[i] += a.coeff[i]; for (int i = 0; i <= b.ord; i++) c.coeff[i] += b.coeff[i]; c.ord = c.order(); return c; } Poly subtraction(Poly b) { Poly a = *this; Poly c; for (int i = 0; i <= a.ord; i++) c.coeff[i] += a.coeff[i]; for (int i = 0; i <= b.ord; i++) c.coeff[i] -= b.coeff[i]; c.ord = c.order(); return c; } int Poly::multiplication(Poly b) { Poly a = *this; Poly c; for (int i = 0; i <= a.ord; i++) for (int j = 0; j <= b.ord; j++) c.coeff[i + j] += (a.coeff[i] * b.coeff[j]); c.order = c.order(); } int main() { Poly a, b, c, d; a.set(7, 4); // 7x^4 a.set(1, 2); // x^2 b.set(6, 3); // 6x^3 b.set(-3, 2); // -3x^2 c = a.subtraction(b); // (7x^4 + x^2) - (6x^3 - 3x^2) c.print(); cout << "\n"; d = c.differentiate().differentiate(); d.print(); cout << "\n"; cout << c.evaluate(2); // substitute x with 2 cin.get(); return 0; }
解决方案Your (first) problem is in this function:
int Poly::addition(Poly b) { Poly a = *this; Poly c; for (int i = 0; i <= a.ord; i++) c.coeff[i] += a.coeff[i]; for (int i = 0; i <= b.ord; i++) c.coeff[i] += b.coeff[i]; c.ord = c.order(); return c; }
You say you will return an int:
int Poly::addition(Poly b) //^------ promise to return int
but you return
c
wherec
is aPoly
. change the return type:Poly Poly::addition(Poly b)
int Poly::differentiate(int)
has the same problem. Change it toPoly Poly::differentiate(int)
BTW - Why does this take a
int
which it ignores?Furthermore,
int Poly::multiplication(Poly b)
doesn't return anything as it stands.You have further problems. For example the print and evaluate functions should be member functions and it may make more sense to use a std::vector to store the coefficients.
The compiler probably told you the line number of the error if you look closely.
这篇关于C ++错误:'没有从[类名称]到“int”的适当转换''的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!