samedi 19 avril 2014

c ++ - le constructeur d'un autre constructeur de la classe même Call - Stack Overflow


I'm having several problems in developing this program I do not understand why.


1) I can not call the constructor method of the class Terna, with the other two methods manufacturers and pass the values ​​to my interests. Line 21 and 22.


2) Should I create two objects using the 'nuovo' method. In class A should create an object of type B. In class B should I create an object of type A. Ritornandoli. In the new method, and you virtual.


#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'



The delegating constructors seem allowed. You only get a warning about it.


The first error is about the fact that you're returning an instance of Terna. It is not allowed. It is considered an abstract class, since you have a pure virtual (abstract in OO terms) method. You can never have a concrete instance of that.


You should use a pointer to Terna instead.


Also note that your main function is called manin. Typo?


Also note that you're using B before it is 'seen' by the compiler. That's why you're getting the


[Error] 'B' was not declared in this scope



If you do that virtual int m() = 0 inside a class you have a abstract class, Abstract classes cannot be used to instantiate objects, but it can be used to create pointers, so nuovo should return a pointer to Terna.




You cannot create an instance of a Terna class due to virtual int m() = 0;


Modify nuovo to return a pointer, not an object.


and of course, compile with -std=c++11


Also, you cannot use the incomplete class B ... while in A::nuovo() ... (you just forward declare with class B; )


Recommendation: split the file up properly in .h, .cpp and then your problems should be solved.




Thank you so much, I've solved some problems. Now give me the following errors:



C:\Users\angel\Desktop\method\method7.cpp In member function 'virtual Terna* A::nuovo(Terna*)':
49 32 C:\Users\angel\Desktop\method\method7.cpp [Error] invalid use of incomplete type 'class B'
41 7 C:\Users\angel\Desktop\method\method7.cpp [Error] forward declaration of 'class B'

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

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){
xyz = new int[3];
xyz[0] = _x;
xyz[1] = _x;
xyz[2] = _x;
}
Terna(int *_x){
xyz = new int[3];
xyz[0] = _x[0];
xyz[1] = _x[1];
xyz[2] = _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 B;

class A : public Terna{

public:
A(int _x) : Terna(_x){}
A(int *_x) : Terna(*_x){}
Terna * nuovo(Terna *_t){
Terna * 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){
Terna * 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 main(){

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();
}

cout << max;

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;
}

From class A can not create an object of type B, can you tell me how I could do. Thanks for your help.



I'm having several problems in developing this program I do not understand why.


1) I can not call the constructor method of the class Terna, with the other two methods manufacturers and pass the values ​​to my interests. Line 21 and 22.


2) Should I create two objects using the 'nuovo' method. In class A should create an object of type B. In class B should I create an object of type A. Ritornandoli. In the new method, and you virtual.


#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'


The delegating constructors seem allowed. You only get a warning about it.


The first error is about the fact that you're returning an instance of Terna. It is not allowed. It is considered an abstract class, since you have a pure virtual (abstract in OO terms) method. You can never have a concrete instance of that.


You should use a pointer to Terna instead.


Also note that your main function is called manin. Typo?


Also note that you're using B before it is 'seen' by the compiler. That's why you're getting the


[Error] 'B' was not declared in this scope


If you do that virtual int m() = 0 inside a class you have a abstract class, Abstract classes cannot be used to instantiate objects, but it can be used to create pointers, so nuovo should return a pointer to Terna.



You cannot create an instance of a Terna class due to virtual int m() = 0;


Modify nuovo to return a pointer, not an object.


and of course, compile with -std=c++11


Also, you cannot use the incomplete class B ... while in A::nuovo() ... (you just forward declare with class B; )


Recommendation: split the file up properly in .h, .cpp and then your problems should be solved.



Thank you so much, I've solved some problems. Now give me the following errors:



C:\Users\angel\Desktop\method\method7.cpp In member function 'virtual Terna* A::nuovo(Terna*)':
49 32 C:\Users\angel\Desktop\method\method7.cpp [Error] invalid use of incomplete type 'class B'
41 7 C:\Users\angel\Desktop\method\method7.cpp [Error] forward declaration of 'class B'

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

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){
xyz = new int[3];
xyz[0] = _x;
xyz[1] = _x;
xyz[2] = _x;
}
Terna(int *_x){
xyz = new int[3];
xyz[0] = _x[0];
xyz[1] = _x[1];
xyz[2] = _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 B;

class A : public Terna{

public:
A(int _x) : Terna(_x){}
A(int *_x) : Terna(*_x){}
Terna * nuovo(Terna *_t){
Terna * 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){
Terna * 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 main(){

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();
}

cout << max;

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;
}

From class A can not create an object of type B, can you tell me how I could do. Thanks for your help.


0 commentaires:

Enregistrer un commentaire