C 方法无法工作

C++ Method wont work

本文关键字:工作 方法      更新时间:2023-10-16

因此,我创建了一种方法,以启动添加,乘法和等多项式的过程,但是,当安排运行start((时,编译器都会运行,但是盒子仍然空白,即使不应该。不确定我在做什么错。

有什么想法?

这是我的代码。

这是我的标题

#ifndef _POLY_GUARD 
#define _POLY_GUARD 
#include <iostream>
using namespace std;
class Polynomial
{
public:
    Polynomial(int coef, int exp);
    void start();   
    friend ostream & operator << (ostream &out, const vector<int> &c);
    friend istream & operator >> (istream &in, const Polynomial &c);  
    void addPolynomials();    
    void multiplyPolynomials();
    void evaluatePolynomial();
    int findCoefficient();
    int findLeadingExponent();    
};
#endif

这是源代码。

#include "Polynomial.h"
#include <utility>
#include <iostream>
#include <vector>
#include <string>
using namespace std;  
void Polynomial::start()
{
    int choice;
    std::cout << "What do you wish to do?" << std::endl;
    std::cout << "1. Add two polynomials" << std::endl;
    std::cout << "2. Multiply two polynomials" << std::endl;
    std::cout << "3. Evaluate one polynomial at a given value" << std::endl;
    std::cout << "4. Find Coefficent for a given polynomial and given exponent" << std::endl;
    std::cout << "5. Find the leading exponent for a given polynomial" << std::endl;
    std::cout << "6. Exit " << std::endl;
    std::cin >> choice;
    if (choice < 1 || choice > 6)
    {
        do
        {
            std::cout << "Invalid entry: please reenter choice" << std::endl;
            std::cin >> choice;   
        } while (choice < 1 || choice > 6);
    }
    if (choice == 1)
    {       
    }
 }

最后,这是我的主要

#include "Polynomial.h"
#include <string>
#include <vector>
#include <utility>
int main()
{
    Polynomial start();
    system("pause");
}

阅读上面的评论与下面的示例一样有用。

因此,您有一个Polynomial类,可以从中创建该特定类型的(实例化(对象。

class Polynomial {
public:
    /// default constructor
    Polynomial() = default;
    /// constructor with your coef and exp parameters
    /// when invoked it will use its arguments to initialize
    /// the data members coef and exp.
    Polynomial(int coef, int exp)
        : coef(coef)
        , exp(exp){};
    /// your member function start()
    void start();
private:
    /// your private data members that 
    /// are initialized upon construction
    /// when calling the appropriate constructor
    int coef;
    int exp;
};

在您的主要功能中,正如其他人提到的那样,您可以构造一个类型Polynomial的对象,称其为appwhatever

int main()
{
    /// app is your object of type Polynomial
    /// its coef and exp are initialized
    /// using your arguments 4 and 5 respectively.
    Polynomial app(4, 5);
    /// now you can call you member function
    app.start();
    return 0;
}

根据此页面,可以在另一个函数中声明功能。

因此,您的main函数声明了一个名为start的函数,该函数返回Polynomial对象,然后调用system("pause"),然后返回。

尝试分开声明和调用方法start

Polynomial p;
p.start();

Polynomial(1, 2).start(); // pass valid parameters

因为

Polynomial start();

是最烦人的解析。

https://en.wikipedia.org/wiki/most_vexing_parse_parse