C++ 为什么要调用隐式删除的默认构造函数?
当我在C++ 为什么要调用隐式删除的默认构造函数?,c++,constructor,default,C++,Constructor,Default,当我在main.cpp中创建Executive对象时,我得到这个错误“调用隐式删除了'Executive'的默认构造函数”。我不知道如何解决它 这是main.cpp: #include <iostream> #include <fstream> #include "Executive.h" using namespace std; int main(int argc, const char * argv[]) { Executive ex; ifstr
main.cpp
中创建Executive对象时,我得到这个错误“调用隐式删除了'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;
}
#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;
}
}
#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++){
}
}
这是Executive.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;
}
#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;
}
}
#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++){
}
}
这是多项式.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;
}
#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;
}
}
#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++){
}
}
#包括“polynomy.h”
#包括
#包括
使用名称空间std;
多项式::多项式(int d,双数组[]){
度=d;
对于(int a=0;a在多项式
类型中,您提供了一些用户定义的构造函数,这意味着编译器不会提供隐式声明的默认构造函数。这反过来意味着您无法创建多项式
对象的原始数组。如果需要使用数组If多项式
,则您需要自己提供一个默认构造函数。您可以创建一个非默认可构造对象的原始数组,但必须提供一个初始值设定项。@bames53:不作为成员变量(至少不在C++03中,可能也不在C++11中,我必须检查类定义内的成员初始化是否允许这样做)@bames53:我刚刚确认:您可以使用数组,但需要在类内的成员定义中提供聚合初始化的初始化列表…现在考虑到类型需要大小和指针,用户可能没有适当的值来为Execu中的所有项提供tive::p
成员定义…所以我甚至不打算将此编辑成答案:)@DavidRodríguez dribeas你是在谈论执行类中的多项式对象吗?可能有某种方法可以使用宏扩展来获得正确数量的初始值设定项。