公共成员函数未返回值。。。我错过什么了吗? 我是C++新手(一般是OOP)。我正在写一个类,它对两个多项式执行一些数学运算。这是我的密码: #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] public: // Poly(); // the default constructor int setOrderAndCoeff(); // sets the order and coefficients int display(); // 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 }; //Poly::Poly() //{ // order = 0; // *coeff = 0; //} int Poly::display() { int i; int j; for (i = order; i >= 0; i--) { cout << coeff[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; int *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(); 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(); 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(); } 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(); cout << "Polynomial 2: "; P2.display(); P3.addition(P1, P2); cout << "--------------------------\n"; break; case 2: cout << "\n-------- Subtraction --------\n"; cout << "Polynomial 1: "; P1.display(); cout << "Polynomial 2: "; P2.display(); P3.subtraction(P1, P2); cout << "--------------------------\n"; break; case 3: cout << "\n-------- Multiplication --------\n"; cout << "Polynomial 1: "; P1.display(); cout << "Polynomial 2: "; P2.display(); 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; }

公共成员函数未返回值。。。我错过什么了吗? 我是C++新手(一般是OOP)。我正在写一个类,它对两个多项式执行一些数学运算。这是我的密码: #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] public: // Poly(); // the default constructor int setOrderAndCoeff(); // sets the order and coefficients int display(); // 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 }; //Poly::Poly() //{ // order = 0; // *coeff = 0; //} int Poly::display() { int i; int j; for (i = order; i >= 0; i--) { cout << coeff[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; int *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(); 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(); 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(); } 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(); cout << "Polynomial 2: "; P2.display(); P3.addition(P1, P2); cout << "--------------------------\n"; break; case 2: cout << "\n-------- Subtraction --------\n"; cout << "Polynomial 1: "; P1.display(); cout << "Polynomial 2: "; P2.display(); P3.subtraction(P1, P2); cout << "--------------------------\n"; break; case 3: cout << "\n-------- Multiplication --------\n"; cout << "Polynomial 1: "; P1.display(); cout << "Polynomial 2: "; P2.display(); 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; },c++,class,C++,Class,2) add是开关案例1中未声明的标识符(与sub和mult的情况相同,但错误相同) 3) 在 无法将参数2转换为int 提前感谢大家的关注 -Ryan您如何期望不返回值的函数返回值?当你有 void addition(Poly P1, Poly P2); 此函数无法返回值。作为第一步,您应该将其更改为 Poly addition(Poly P1, Poly P2); 并在函数中添加适当的return语句。但它仍然不是通常在C++中的方式。您不希望通过值传递Poly对象,因为这样做成本高,并

2) add是开关案例1中未声明的标识符(与sub和mult的情况相同,但错误相同)

3) 在

无法将参数2转换为int

提前感谢大家的关注


-Ryan

您如何期望不返回值的函数返回值?当你有

void addition(Poly P1, Poly P2);
此函数无法返回值。作为第一步,您应该将其更改为

Poly addition(Poly P1, Poly P2);
并在函数中添加适当的
return
语句。但它仍然不是通常在C++中的方式。您不希望通过值传递
Poly
对象,因为这样做成本高,并且使用当前定义会泄漏内存。你应该使用

Poly addition(const Poly& P1, const Poly& P2);
最后也是最重要的一句话。不要使用指针。替换你的

int *coeff; 

std::向量系数;

只有在您知道自己在做什么以及在低级别实现中,才应该使用手动内存分配。出于您的目的,
std::vector
要好得多。

我假设
返回的意思是
显示的
。问题是
sub
add
mult
Poly::display
函数中不可见。简单地说,尝试将此变量添加为
Poly
类的成员

class Poly
{

private:

    int order;                                  // the order of the polynomial
    int *coeff;                                 // pointer to an array of coefficients
    int *add, *sub, *mult;                       

// ...
};
然后,您不必在每个函数中声明它们,例如在
Poly::addition(Poly P1,Poly P2)
中,您可以更改:

int *add = new int[max + 1];

编辑:在函数
显示中显示
coeff
内容,调用任何操作时,
coeff
不改变

您必须对函数
display
进行一些修改,最简单的方法是重新定义display以接受2个参数:

class Poly
{

puplic:
    // ...
    void display(int *data, int count);
    // ...
};

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

对这些函数中的每一个都缺少一个非void返回类型和一个return语句。@OP,使用Vectors!如果你沿着这条路走下去,你将需要一个@user3407254-下次,请使用增量方法开发一个程序。您编写了所有这些函数(加法、减法、乘法),而没有在构建时对每个函数进行测试。因此,现在您不知道错误可能在哪里,因为您一次性编写了整个代码。@user3407254-此外,您的代码到处都会泄漏内存。每次选择一个选项不止一次时都会出现内存泄漏,更不用说注释掉析构函数,从而导致更多问题。使用std::vector,因为目标应该是创建多项式解算器,而不是动态数组类。是的,我的意思是“显示”。很抱歉。当我添加“*add”、“*sub”和“*mult”作为私有成员变量,并将“int*add=new int[max+1];”替换为“add=new int[max+1];”时,我看到了相同的行为(当我选择“1”调用加法时,结果多项式不会显示。有什么想法吗?谢谢。我已经根据您的建议更新了代码。在“int Poly::display”中(int*data,int count)”,我认为我是未定义的。我们如何定义它?我已经编辑了文章以包含更新的代码。提前谢谢。@user3407254对不起,我的错误,在foor循环的显示函数中必须读取(int I=count…)的
。注意
int
关键字。另一方面,for循环的int又是count no coutOK,我已经更新了for循环(int I和count而不是cout)。当我尝试构建时,我看到“display(add,count)”给出了一个错误,该错误为“error C2664:'int Poly::display(int*,int)“:无法将参数2从'iterator_traits::difference_type(u cdecl*)(_InIt,_InIt,const _Ty&)'转换为'int'。知道是什么导致了这一点吗?我已经发布了上面的修订代码。再次感谢(我将继续投票)@user3407254-您没有在调用display时发布
count
真正是什么。您所做的只是描述它是什么。它肯定不是一个简单的整数——如果是,那么就不应该发生错误,或者
display
函数不是您正在向我们显示的函数。其次,重复一下,您的代码可能“工作”,但正如其他评论和答案所述,这是一个充满错误的拼凑。
std::vector<int> coeff;
class Poly
{

private:

    int order;                                  // the order of the polynomial
    int *coeff;                                 // pointer to an array of coefficients
    int *add, *sub, *mult;                       

// ...
};
int *add = new int[max + 1];
add = new int[max + 1]; // Or 
                        // this->add = new int[max + 1];
                        // if you prefer.
class Poly
{

puplic:
    // ...
    void display(int *data, int count);
    // ...
};

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;
}
void Poly::addition(Poly P1, Poly P2)
{
    //...
    display(add, count)  // Here count is the ammount if elements in data.
}