代码之家  ›  专栏  ›  技术社区  ›  Nom OnTheCookie

C++方法不起作用

  •  -2
  • Nom OnTheCookie  · 技术社区  · 8 年前

    因此,我创建了一个方法,以启动加法、乘法等多项式的过程,然而,当尝试运行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");
    }
    
    3 回复  |  直到 8 年前
        1
  •  1
  •   eric    8 年前

    阅读上面的评论,因为它们与下面的示例一样有用。

    所以你有一门课 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;
    };
    

    正如其他人所提到的,在main函数中,可以构造类型为 多项式 ,调用它 app whatever :

    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;
    }
    
        2
  •  1
  •   xvnm    8 年前

    根据 this page ,可以在另一个函数中声明函数。

    所以你的 main 函数声明一个名为 start 返回 Polynomial 对象和调用 system("pause") ,然后返回。

    尝试划分声明和调用方法 开始 :

    Polynomial p;
    p.start();
    

    Polynomial(1, 2).start(); // pass valid parameters
    
        3
  •  0
  •   Jay Wai Tan    8 年前

    因为

    Polynomial start();
    

    是一个非常烦人的解析。

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