C++错误:无法将参数 1 从 'int' 转换为 'const [类名]
C++ error: cannot convert argument 1 from 'int' to 'const [class name]
我正在写一个类来加,减和乘多项式。我看到以下错误:
错误C2664: 'Poly::Poly(const Poly &)':无法将参数1从'int'转换为'const Poly '
来自这个语句:
return p; // in the evaluate() function
有人知道如何纠正这个吗?
事先非常感谢。对
完整代码:
#include "stdafx.h"
#include <iostream>
#include <string>
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
Poly addition(Poly b); // adds 2 polynomials
Poly subtraction(Poly b); // subtracts 2 polynomials
Poly multiplication(Poly b); // multiplies 2 polynomials
Poly evaluate(int); // uses Horner's method to compute and return the polynomial evaluated at x
Poly differentiate(); //
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 << " ";
}
}
}
Poly Poly::evaluate(int x)
{
int p = 0;
for (int i = ord; i >= 0; i--)
p = coeff[i] + (x * p);
return p;
}
Poly Poly::differentiate()
{
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;
}
Poly 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 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;
}
Poly 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.ord = c.order();
return c;
}
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;
}
函数返回int
,但应该返回Poly
。
我已经改变了你的代码来解决构建错误。请检查diff,修改后的代码是
#include "stdafx.h"
#include <iostream>
#include <string>
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
Poly addition(Poly b); // adds 2 polynomials
Poly subtraction(Poly b); // subtracts 2 polynomials
Poly multiplication(Poly b); // multiplies 2 polynomials
int evaluate(int); // uses Horner's method to compute and return the polynomial evaluated at x
Poly differentiate(); //
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 Poly::print()
{
int coeff[] = { 0 };
for (int i = 99; i >= 0; i--)
{
if (coeff[i] != 0)
{
cout << coeff[i] << "x^" << i << " ";
}
}
}
int Poly::evaluate(int x)
{
int p = 0;
for (int i = ord; i >= 0; i--)
p = coeff[i] + (x * p);
return p;
}
Poly Poly::differentiate()
{
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;
}
Poly 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 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;
}
Poly 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.ord = c.order();
return c;
}
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;
}
相关文章:
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 是否可以从int转换为enum类类型
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- 功能样式转换从 'int' 到 'ItemType' 的匹配转换
- 将 int 数组转换为 std::vector<int*>
- 如何转换 int 变量并附加到常量 wchar_t*
- 类型转换<int>的舍入问题
- 在Objective-C中调用sqlite3数据库时,如何调用和转换int/NSNumber
- 转换 int -> 无符号长 长 是否由标准定义
- 简单的 int 到字符串转换..int to const char* 无效
- 如何将转换 int 除法键入为浮点数
- C++类型转换 int * 到类
- 预期的类型说明符,并且无法在初始化时转换“int*”
- 转换int并将其与char*连接
- 字符串流在转换int型时添加新行
- 转换int为字符串/char c++ /Arduino
- c# P-Invoke:如何转换INT fun(BYTE *bStream, UINT16 *nCount, const
- 类型转换(int**)和int(*)的区别
- 从"int"到"nodeType"的无效转换<int>
- 从无符号int构造对强制转换int的引用