从同一个类的另一个构造函数中调用构造函数

时间:2014-02-13 12:48:55

标签: c++ c++11 constructor virtual

我在开发这个程序时遇到了一些问题,我不明白为什么。

1)我无法调用Terna类的构造函数方法,使用其他两种方法制造商并将值传递给我的兴趣。第21和22行。

2)我应该使用'nuovo'方法创建两个对象。 在类A中应该创建一个B类型的对象 在B类中,我应该创建一个A类型的对象。 Ritornandoli。 在新方法中,你是虚拟的。

#include <iostream>
#include <stdlib.h>
#include <sstream>
#define DEF 50
using namespace std;

class B;

class Terna{

    protected:
        int * xyz;

    public:
        Terna(int _x,int _y,int _z){
            xyz = new int[3];
            xyz[0] = _x;
            xyz[1] = _y;
            xyz[2] = _z;
        }
        Terna(int _x) : Terna(_x, _x, _x){}
        Terna(int *_x) : Terna(_x[0],_x[1],_x[2]){}
        string toString(){
            stringstream t;
            t << "[ " << xyz[0] << ", " << xyz[1] << ", " << xyz[2] <<" ]";
            return t.str();
        }
        virtual int m() = 0;
        //virtual Terna nuovo(Terna *_t) = 0;

};

class A : public Terna{

    public:
        A(int _x) : Terna(_x){}
        A(int *_x) : Terna(*_x){}
        Terna nuovo(Terna *_t){
            B * b = new B(m()+xyz[0]);
            return b;
        }
        int m(){
            int m = 0;
            for(int i=0;i<3;i++)
                m += xyz[i];
            return m/3;
        }
        string toString(){
            stringstream s;
            for(int i=0;i<m();i++)
                s << 'x';
            return "A:" + Terna::toString()+" "+s.str();
        }
};

class B : public Terna{

    public:
        B(int _x) : Terna(_x){}
        B(int *_x) : Terna(*_x){}
        Terna nuovo(Terna *_t){
            A * a = new A(m()-xyz[1]-xyz[2]);
            return a;
        }
        int m(){
            int max = 0;
            for(int i=0;i<3;i++)
                if(max<xyz[i])
                    max = xyz[i];
            return max;
        }
        string toString(){
            stringstream s;
            for(int i=0;i<m();i++)
                s << 'x';
            return "B:" + Terna::toString()+" "+s.str();
        }
};


int manin(){

    srand(2999888);
    Terna * vett[DEF];
    int * x = new int[3];
    int max = 0;
    for(int i=0;i<DEF;i++){
        for(int j=0;j<3;j++) 
            x[j] = rand()%10;
        if(rand()%2 == 1) vett[i] = new A(x);
        else vett[i] = new B(x);

        if(max<vett[i]->m())
            max = vett[i]->m();
    }

    Terna * vett2[DEF/2];
    vett2[0] = vett[0];
    vett2[1] = vett[DEF];

    for(int i=2;i<DEF;i++)
        vett2[i] = vett2[i-1]->nuovo(vett2[i-2]);

    return 0;
}
C:\Users\angel\Desktop\method\method7.cpp   In constructor 'Terna::Terna(int)':
19  35  C:\Users\angel\Desktop\method\method7.cpp   [Warning] delegating constructors only available with -std=c++11 or -std=gnu++11 [enabled by default]
C:\Users\angel\Desktop\method\method7.cpp   In constructor 'Terna::Terna(int*)':
20  43  C:\Users\angel\Desktop\method\method7.cpp   [Warning] delegating constructors only available with -std=c++11 or -std=gnu++11 [enabled by default]
C:\Users\angel\Desktop\method\method7.cpp   At global scope:
36  9   C:\Users\angel\Desktop\method\method7.cpp   [Error] invalid abstract return type for member function 'Terna A::nuovo(Terna*)'
7   7   C:\Users\angel\Desktop\method\method7.cpp   [Note] because the following virtual functions are pure within 'Terna':
26  15  C:\Users\angel\Desktop\method\method7.cpp   [Note] virtual int Terna::m()
C:\Users\angel\Desktop\method\method7.cpp   In member function 'Terna A::nuovo(Terna*)':
36  9   C:\Users\angel\Desktop\method\method7.cpp   [Error] invalid abstract return type for member function 'Terna A::nuovo(Terna*)'
7   7   C:\Users\angel\Desktop\method\method7.cpp   [Note] since type 'Terna' has pure virtual functions
37  4   C:\Users\angel\Desktop\method\method7.cpp   [Error] 'B' was not declared in this scope
37  8   C:\Users\angel\Desktop\method\method7.cpp   [Error] 'b' was not declared in this scope
37  16  C:\Users\angel\Desktop\method\method7.cpp   [Error] expected type-specifier before 'B'
37  16  C:\Users\angel\Desktop\method\method7.cpp   [Error] expected ';' before 'B'
C:\Users\angel\Desktop\method\method7.cpp   At global scope:
59  9   C:\Users\angel\Desktop\method\method7.cpp   [Error] invalid abstract return type for member function 'Terna B::nuovo(Terna*)'
7   7   C:\Users\angel\Desktop\method\method7.cpp   [Note] since type 'Terna' has pure virtual functions
C:\Users\angel\Desktop\method\method7.cpp   In member function 'Terna B::nuovo(Terna*)':
59  9   C:\Users\angel\Desktop\method\method7.cpp   [Error] invalid abstract return type for member function 'Terna B::nuovo(Terna*)'
7   7   C:\Users\angel\Desktop\method\method7.cpp   [Note] since type 'Terna' has pure virtual functions
61  11  C:\Users\angel\Desktop\method\method7.cpp   [Error] invalid conversion from 'A*' to 'int' [-fpermissive]
19  3   C:\Users\angel\Desktop\method\method7.cpp   [Error] initializing argument 1 of 'Terna::Terna(int)' [-fpermissive]
61  11  C:\Users\angel\Desktop\method\method7.cpp   [Error] cannot allocate an object of abstract type 'Terna'
7   7   C:\Users\angel\Desktop\method\method7.cpp   [Note] since type 'Terna' has pure virtual functions
C:\Users\angel\Desktop\method\method7.cpp   In function 'int manin()':
100 26  C:\Users\angel\Desktop\method\method7.cpp   [Error] 'class Terna' has no member named 'nuovo'

3 个答案:

答案 0 :(得分:2)

如果你在类中有virtual int m() = 0你有一个抽象类,抽象类不能用于实例化对象,但它可以用来创建指针,所以nuovo应该返回一个指向Terna

答案 1 :(得分:0)

由于Terna

,您无法创建virtual int m() = 0;班级的实例

修改nuovo以返回指针,而不是对象。

当然,使用-std=c++11

进行编译

此外,您无法在B中使用不完整的课程A::nuovo() ...(您只需使用class B;转发声明)

建议:在.h.cpp中正确拆分文件,然后解决您的问题。

答案 2 :(得分:0)

委托构造函数似乎被允许。你只会收到警告。

第一个错误是关于你要返回Terna实例的事实。这个不允许。它被认为是一个抽象类,因为你有一个纯虚拟(OO术语中的抽象)方法。你永远不可能有一个具体的例子。

你应该使用指向Terna的指针。

另请注意,您的主要功能称为manin。错字?

另请注意,在编译器“看到”之前,您正在使用B.这就是你获得

的原因
[Error] 'B' was not declared in this scope