C ++错误:'没有从[类名]转换为" int" '

时间:2014-08-01 16:53:16

标签: c++ class

我正在研究多项式的加法,减法和乘法。

我收到以下错误:

'从[类名]到“int”'

没有合适的转换函数

在声明中:

return c;    (addition function)

return deriv;

任何人都可以就如何纠正这些提供任何指导吗?

非常感谢, 莱恩

这是我的代码:

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

2 个答案:

答案 0 :(得分:1)

编译器告诉你确切的问题。 声明添加方法返回int,而var c是类Poly的实例。 您需要返回int

答案 1 :(得分:1)

你的(第一个)问题出现在这个函数中:

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

你说你将返回一个int:

int Poly::addition(Poly b)
//^------ promise to return int

但是您返回c,其中cPoly。更改返回类型:

Poly Poly::addition(Poly b)

int Poly::differentiate(int)有同样的问题。 将其更改为

Poly Poly::differentiate(int)

BTW - 为什么这会忽略它忽略的int

此外,int Poly::multiplication(Poly b)不会返回任何内容。

你还有其他问题。例如,print和evaluate函数应该是成员函数,使用std :: vector存储系数可能更有意义。

如果仔细观察,编译器可能会告诉你错误的行号。