为什么我会调用implicitly_deleted默认构造函数?

时间:2013-09-13 01:29:55

标签: c++ constructor default

我收到此错误"调用“执行”的hiddenly_deleted默认构造函数'当我在main.cpp中创建Executive对象时。我不知道如何解决它。

这是main.cpp

#include <iostream>
#include <fstream>
#include "Executive.h"
using namespace std;

int main(int argc, const char * argv[])
{

    Executive ex;
    ifstream num("Lab01Polynomial.txt");
    ifstream com("Lab01Directives");
    ex.readFiles(num ,com);
    num.close();
    com.close();
    return 0;
}

这是Executive.h

#ifndef lab1_eecs_Executive_h
#define lab1_eecs_Executive_h
#include <fstream>
#include "Polynomial.h"

class Executive{
private:

    static const int MAX_DEGREE = 20;
    static const int MAX_NUM_POLYNOMIALS = 10;
    double coeff[MAX_DEGREE];
    Polynomial p[MAX_NUM_POLYNOMIALS];

public:
    void readFiles(std::istream &inpNumber, std::istream &inpInstructions);


};


#endif

这是Executive.cpp

#include "Executive.h"
#include <fstream>
#include <iostream>
#include "Polynomial.h"
using namespace std;

void Executive::readFiles(std::istream &inpNumber, std::istream &inpInstructions){
    int num_polynomials = 0;
    double coef;
    int poly_size;
    double array[MAX_DEGREE];

    while(!inpNumber.eof() && num_polynomials <= MAX_NUM_POLYNOMIALS){
        inpNumber >> poly_size;
        for(int a = 0; a <= poly_size; a++){
            inpNumber >> coef;
            array[a] = coef;
        }
        Polynomial poly(poly_size, array);
        p[num_polynomials] = poly;
        num_polynomials++;
    }

    string command;
    int poly_index;

    while( !inpInstructions.eof()){
        inpInstructions >> command;
        inpInstructions >> poly_index;

        if(poly_index > num_polynomials){
            cout << "The polynomial doesn't exist" << endl;
        }else if (command == " add"){
            int poly2_index;
            inpInstructions >> poly2_index;
            p[poly_index].add(p[poly2_index]);
        }else if (command == "evaluate"){
            double plugin_value;
            inpInstructions >> plugin_value;
            p[poly_index].evaluate(plugin_value);
        }else if (command == "differentiate"){
            p[poly_index].differentiate();

        }else if(command == "print"){
            p[poly_index].print(std::cout);

        }else
            cout << "Please type a command"<< endl;
    }
}

这是Polynomial.h

#ifndef lab1_eecs_Polynomial_h
#define lab1_eecs_Polynomial_h
#include <fstream>
using namespace std;

class Polynomial{
private:
    static const int MAX_DEGREE = 20;
    double array_polynomial[MAX_DEGREE];
    int degree;

public:
    Polynomial(int d, double a[]);
    double evaluate(double x) const;
    Polynomial add(Polynomial p2) const;
    void print(std::ostream &os) const;
    Polynomial differentiate() const;

};
#endif

这是Polynomial.cpp

#include "Polynomial.h"
#include <iostream>
#include <math.h>
using namespace std;


Polynomial::Polynomial(int d, double array[]){
    degree = d;
    for(int a =0; a <= degree; a++){
        array_polynomial[a] = array[a];
    }
}

double Polynomial::evaluate(double x) const{
    double final_value = 0;

    for(int a = 0; a <= degree; a++){
        final_value += array_polynomial[a] * pow(x, a);
    }

    cout << "The polynomial ";
    for(int b = 0; b <= degree; b++){
        if(b == 0){
            cout << array_polynomial[b] << " + ";
        }
        if(b == 1){
            cout << array_polynomial[b] << "x";
        }

        cout << " + " << array_polynomial[b] << "x^" << b;
    }
    cout << " when the value " << x << " is plugged in is " << final_value << endl;;
}

Polynomial Polynomial::add(Polynomial p2) const{
    double sum_array[degree];
    for(int a = 0; a <= degree; a++){
        sum_array[a] = array_polynomial[a] + p2.array_polynomial[a];
    }
    for(int a = 0; a <= degree; a++){
        if ( a== 0){
            cout << sum_array[a] << " + ";
        }else if ( a == 1){
            cout << sum_array[a] << "x";
        }else{
            cout << " + " << sum_array[a] << "x^" << a;
        }
        cout << endl;
    }
}

void Polynomial::print(std::ostream &os) const{
    for(int a = 0; a <= degree; a++){
        if(a == 0){
            os << array_polynomial[a] << " + ";
        }
        else if ( a == 1){
            os << array_polynomial[a] << "x";
        }else{
            os << " + " << array_polynomial[a] << "x^" <<a;
        }
    }
}

Polynomial Polynomial::differentiate() const{
    for(int a = 0; a <= degree; a++){

    }
}

任何帮助都会非常有用。

1 个答案:

答案 0 :(得分:1)

Polynomial类型中,您提供了一些用户定义的构造函数,这意味着编译器不会提供隐式声明的默认构造函数。这反过来意味着您无法创建Polynomial个对象的原始数组。如果你需要使用Polynomial的数组,那么你需要自己提供一个默认的构造函数。