【问题标题】:Error when creating a class Object创建类对象时出错
【发布时间】:2018-04-11 20:58:09
【问题描述】:

我正在尝试创建一个对象,但显然它给构造函数带来了某种错误……什么? 我的代码如下所示:

#include <iostream>
#include "Pila.h"
#include "Functions.cpp"
#include <cstring>
using namespace std;
int error = 0;
class Hyogen{
private:
    char *kozui;
    int p_close;
    int p_open;
    int err_par, err_op, err_end, err_start;
public:
    Hyogen(char *a);
    bool Verify();
    char *Elaborate();
    void Errors();
};

Hyogen::Hyogen(char *a){
    kozui = a;
    err_par = 0;
    err_op = 0;
    err_end = 0;
    err_start = 0;
    p_close = 0;
    p_open = 0;
}

bool Hyogen::Verify(){
    for(int i=0;i<strlen(kozui);i++){
        if(i == strlen(kozui)){
            if(error != 0){
                cout<<"uwu";
                return false;
                break;}
            else{
                cout<<"owo";
                return true;}
            }

        check_parentesis(kozui, p_close, p_open);

        if(p_close > p_open || p_open > p_close){
            error++;
            err_par++;
        }

        if(i == 0)
            if(check_start_various(kozui)){
                error++;
                err_start++;
            }

        if(check_operator(kozui)){

            if(check_mul_div(kozui))
                break;

            if(check_doubleop(kozui)){
                error++;
                err_op++;
            }
        }

        if(i == strlen(kozui))
            if(check_end_various(kozui)){
                err_end++;
                error++;
            }
    }
}

int main()
{
    Hyogen koz;
    char *espressione;
    espressione = new char[50];
    cout<<"Inserisci l'espressione: ";
    cin>>espressione;
    return 0;
}

我得到的错误如下:

main.cpp: In function ‘int main()’:
main.cpp:76:12: error: no matching function for call to ‘Hyogen::Hyogen()’
     Hyogen koz;
            ^~~
main.cpp:20:1: note: candidate: Hyogen::Hyogen(char*)
 Hyogen::Hyogen(char *a){
 ^~~~~~
main.cpp:20:1: note:   candidate expects 1 argument, 0 provided
main.cpp:7:7: note: candidate: constexpr Hyogen::Hyogen(const Hyogen&)
 class Hyogen{
       ^~~~~~
main.cpp:7:7: note:   candidate expects 1 argument, 0 provided
main.cpp:7:7: note: candidate: constexpr Hyogen::Hyogen(Hyogen&&)
main.cpp:7:7: note:   candidate expects 1 argument, 0 provided

发生了什么事?我需要做什么?似乎我必须在创建对象时提供一个参数,但是如何? (我需要通过“espressione”)

【问题讨论】:

  • 但要解决您的问题,请将Hyogen koz; 移动到您获得输入的位置之后,然后更改为Hyogen koz(espressione);
  • 它仍然报错,我试过了:/ 编辑:它成功了,非常感谢!

标签: c++ arrays class compiler-errors


【解决方案1】:

行内:

Hyogen koz;

您使用默认构造函数创建类的对象,而类定义中没有该构造函数。您必须定义默认构造函数:

Hyogen() { };

或在 C++11 中使用:

Hyogen() = default; 

【讨论】:

    【解决方案2】:
    int main()
    {
        //Hyogen koz;        Move this
        char *espressione;
        espressione = new char[50];
        cout<<"Inserisci l'espressione: ";
        cin>>espressione;
        Hyogen koz(espressione);    // To here and add parameter
        return 0;
    }
    

    DEMO(删除了一些无关代码)。

    【讨论】:

      【解决方案3】:
      #include <iostream>
      #include "Pila.h"
      #include "Functions.cpp"
      #include <cstring>
      using namespace std;
      int error = 0; 
      class Hyogen{
      private:
          char *kozui;
          int p_close;
          int p_open;
          int err_par, err_op, err_end, err_start; public:
          Hyogen(char *a);
          bool Verify();
          char *Elaborate();
          void Errors(); };
      public:
          Hyogen() {}
      //  ^^^^^^^^^^^   
          Hyogen(char *a);
          bool Verify();
          char *Elaborate();
      
          void Errors();
      };
      

      正如在 cmets 中已经回答的那样,您需要一个默认构造函数

      ClassName ( ) ;
      ClassName() = default ;
      

      还有其他选择怎么做

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-11-20
        • 1970-01-01
        • 2021-01-10
        • 2015-01-20
        • 2020-07-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多