Sign in to follow this  

what is wrong here?

This topic is 4689 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

CPerson* CPerson::RandomObjectCreator() //this is class static function
{
	switch(rand()%NumberOfClassTypes)
	{
	case 0:                //all other class are derived from CPerson..
		{
		    return (new CPerson);
		}
		break;
	case 1:
		{
		    return (new CStudent);//return dynamic_cast<CPerson*>(new CStudent);
		}
		break;
	case 2:
		{
		    return (new CTeacher  );
		}
		break;
	case 3:
		{
		    return (new CWizard);
		}
		break;
	case 4:
		{
		    return (new CWorker);
		}
		break;
	case 5:
		{
		    return (new CDwarf);
		}
		break;
	case 6:
		{
			return (new CKnight);
		}
		break;
	}
}

all class are derived from CPerson and no multiple inheritance is used a want to have generator (which will be static funciton) of base class..

Share this post


Link to post
Share on other sites
What is the actualy wrong with it? What errors are you getting?

Not all paths lead to return statement. Add another return NULL; or return new CPerson() at the end.

healeyx76: rand returns int

Share this post


Link to post
Share on other sites
My problem is not about switch() case .its just a example code.
MyProblem is when i made a class hiearchy (a simple one no multiple inheritance is used) and tried to have a generator(static function of Base Class) compiler gives an error no "approprate default constructor found" ..although there is ..
and it is not helping me to return an CStudent* as an CPerson*.it is not also helping me to convert with static/dynamic casting...so i can't get my generator completed.although when generator is a global its okey ...

Share this post


Link to post
Share on other sites
here is a code for example a complete code try to compile it.(there can't be an error at compiler.. i am sure..)

#include <strstream>
#include <iostream>
#include <fstream>
#include <typeinfo>
#include <iomanip>

using namespace std;

//////////////////////////////CONSTANTS & GLOBALS & ENUMS /////////////////////////////////////
const int ENORMOUS=256;
const int MAX=80;
const int MIN=10;

char RandomBuffer[ENORMOUS];//For random access of anypart of program
enum ClassType {NULLCLASS=0,CPersonClass=1,CStudentClass,CWorkerClass,CTeacherClass,CDwarfClass
,CKnightClass,CWizardClass};

class CPerson;
class CStudent;
class CWorker;
class CTeacher;
class CDwarf;
class CKnight;
class CWizard;


//////////////////////////////////**** CLASS & STRUCTURES **** ///////////////////////////////////

/* CLASS CPERSON is the base class of hierarchy and have virtual functions for
overloading << and >> in its child classes.(overloaded << and >> are not virtual
because they are not member of the class (it is impossible..)
but the printon() and geton() functions that are making the actual work in them
are virtual */

class CPerson
{
protected:
ClassType ObjectType;
static int NumberOfClassTypes;
static void InitializeNameBuffer(CPerson* AnyPerson=NULL);
static ClassType ClassTypeOfObject(CPerson* AnyPerson=NULL);
static CPerson* RandomObjectCreator();
char Name[MAX];
int Age;
double Length;
double Weight;
virtual void GetOn(istream& AnyStream);//takes all the important info for overloaded <<
virtual void PrintOn(ostream& AnyStream); //prints all the improtant info for overloaded >>
public:
CPerson():Age(0),Length(0.0),Weight(0.0)
{
ObjectType=ClassTypeOfObject(this);
InitializeNameBuffer(this);
}
friend istream& operator >>(istream& AnyStream,CPerson& AnyPerson)
{
AnyPerson.GetOn(AnyStream);
return AnyStream;
}
friend ostream& operator <<(ostream& AnyStream,CPerson& AnyPerson)
{
AnyPerson.PrintOn(AnyStream);
return AnyStream;
}
virtual ~CPerson()
{
}
};
int CPerson::NumberOfClassTypes=7;
void CPerson::InitializeNameBuffer(CPerson* AnyPerson) //this is class static function
{
for(register int cntr1=0;cntr1<MAX;++cntr1)
{
*(AnyPerson->Name+cntr1)=NULL;
}
}
ClassType CPerson::ClassTypeOfObject(CPerson* AnyPerson) //this is class static function
{
if(typeid(CPerson)==typeid(*AnyPerson))
return CPersonClass;
else if(typeid(CStudent)==typeid(*AnyPerson))
return CStudentClass;
else if(typeid(CWorker)==typeid(*AnyPerson))
return CWorkerClass;
else if(typeid(CTeacher)==typeid(*AnyPerson))
return CTeacherClass;
else if(typeid(CDwarf)==typeid(*AnyPerson))
return CDwarfClass;
else if(typeid(CKnight)==typeid(*AnyPerson))
return CKnightClass;
else if(typeid(CWizard)==typeid(*AnyPerson))
return CWizardClass;
else
return NULLCLASS;
}
CPerson* CPerson::RandomObjectCreator() //this is class static function
{
switch(rand()%NumberOfClassTypes)
{
case 0: //all other class are derived from CPerson..
{
return (new CPerson);
}
break;
case 1:
{
return (new CStudent);//return dynamic_cast<CPerson*>(new CStudent);
}
break;
case 2:
{
return (new CTeacher );
}
break;
case 3:
{
return (new CWizard);
}
break;
case 4:
{
return (new CWorker);
}
break;
case 5:
{
return (new CDwarf);
}
break;
case 6:
{
return (new CKnight);
}
break;
}
}



void CPerson::GetOn(istream& AnyStream) //For help go to www.parashift.com//C++ Faq 15.9
{
bool isConsole=(AnyStream==cin);
if(isConsole)
{
cout<<"\nEnter Name:";
}
while(!AnyStream.getline(Name,MAX,'\n'))
{
AnyStream.clear();
AnyStream.ignore(MAX,'\n');
if(isConsole)
cout<<"Type Name Correctly..Name can not be bigger than 80";
}
if(isConsole)
{
cout<<"Enter Age:";
}
while(!(AnyStream>>Age))
{
AnyStream.clear();
AnyStream.ignore(MAX,'\n');
}
AnyStream.ignore(MAX,'\n');
if(isConsole)
cout<<"Enter Length:";
while(!(AnyStream>>Length))
{
AnyStream.clear();
AnyStream.ignore(MAX,'\n');
}
AnyStream.ignore(MAX,'\n');
if(isConsole)
cout<<"Enter Weight:";
while(!(AnyStream>>Weight))
{
AnyStream.clear();
AnyStream.ignore(MAX,'\n');
}
AnyStream.ignore(MAX,'\n');
}
void CPerson::PrintOn(ostream& AnyStream)
{
AnyStream.setf(ios::left|ios::fixed);
AnyStream.precision(2);
AnyStream<<"\nClass Type: "<<ObjectType;
AnyStream<<setw(MIN)<<"\nName: "<<Name;
AnyStream<<setw(MIN)<<"\nAge : "<<Age;
AnyStream<<setw(MIN)<<"\nLength: "<<Length;
AnyStream<<setw(MIN)<<"\nWeight: "<<Weight;
AnyStream.unsetf(ios::left|ios::fixed);
}

/* Class CStudent is just an unimprotant class..it exist only
for compability and variety.only variable it differantiate from its parents are
grades.. */

class CStudent:virtual public CPerson
{
protected:
enum { MAXGRADE=10}; //For That Class it is a static constant
unsigned int OneHandWeaponFightingGrade;
unsigned int TwoHandWeaponFightingGrade;
unsigned int WizardyGrade;
unsigned int AlchemyGrade;
void GetOn(istream& AnyStream); //override actual function of overloaded operator >>
void PrintOn(ostream& AnyStream);//override actual functionof overloaded operator <<
public:
CStudent():CPerson(),OneHandWeaponFightingGrade(0),TwoHandWeaponFightingGrade(0),
WizardyGrade(0),AlchemyGrade(0)
{
ObjectType=ClassTypeOfObject(this); //must be overridden otherwise CPerson contructor
} //sets that value as its a CPerson Object
virtual ~CStudent() { }
};
void CStudent::GetOn(istream& AnyStream)
{
bool IsConsole=(AnyStream==cin);
char* Errors="Grades are numeric values and can't bo smaller than 0";
CPerson::GetOn(AnyStream);
if(IsConsole)
cout<<"Enter OneHandWeaponFightingGrade:";
while(!(AnyStream>>OneHandWeaponFightingGrade) || (OneHandWeaponFightingGrade<0))
{
if(IsConsole)
cout<<Errors;
AnyStream.clear();
AnyStream.ignore(MAX,'\n');
}
AnyStream.ignore(MAX,'\n');
if(IsConsole)
cout<<"Enter TwoHandWeaponFightingGrade:";
while(!(AnyStream>>TwoHandWeaponFightingGrade) || (TwoHandWeaponFightingGrade<0))
{
if(IsConsole)
cout<<Errors;
AnyStream.clear();
AnyStream.ignore(MAX,'\n');
}
AnyStream.ignore(MAX,'\n');
if(IsConsole)
cout<<"Enter WizardyGrade:";
while(!(AnyStream>>WizardyGrade) || (WizardyGrade<0))
{
if(IsConsole)
cout<<Errors;
AnyStream.clear();
AnyStream.ignore(MAX,'\n');
}
AnyStream.ignore(MAX,'\n');
if(IsConsole)
cout<<"Enter AlchemyGrade:";
while(!(AnyStream>>AlchemyGrade) && (AlchemyGrade>0))
{
if(IsConsole)
cout<<Errors;
AnyStream.clear();
AnyStream.ignore(MAX,'\n');
}
AnyStream.ignore(MAX,'\n');
}
void CStudent::PrintOn(ostream& AnyStream)
{
bool IsConsole=(AnyStream==cin);
CPerson::PrintOn(AnyStream);
AnyStream.setf(ios::fixed|ios::left);
AnyStream.precision(2);
cout<<setw(MIN+MIN)<<"\nOneHandWeaponFigting Grade: "<<OneHandWeaponFightingGrade;
cout<<setw(MIN+MIN)<<"\nTwoHandWeaponFigting Grade: "<<TwoHandWeaponFightingGrade;
cout<<setw(MIN+MIN)<<"\nWizardy Grade: "<<WizardyGrade;
cout<<setw(MIN+MIN)<<"\nAlchemy Grade: "<<AlchemyGrade;
}

/* CWorker class is an unimportant class to it has weeklyworking hours more than
CPerson and nothing more...*/

class CWorker:virtual public CPerson
{
private:
unsigned int TotalWeeklyWorkingHours;
unsigned int Salary;
void GetOn(istream& AnyStream);
void PrintOn(ostream& AnyStream);
public:
CWorker():CPerson(),TotalWeeklyWorkingHours(0),Salary(0)
{
ObjectType=ClassTypeOfObject(this);
}
virtual ~CWorker() { }

};
void CWorker::GetOn(istream& AnyStream)
{
bool isConsole=(AnyStream==cin);
CPerson::GetOn(AnyStream);
if(isConsole)
cout<<"Enter TotalWeeklyWorkingHours:";
while(!(AnyStream>>TotalWeeklyWorkingHours))
{
AnyStream.clear();
AnyStream.ignore(MAX,'\n');
}
AnyStream.ignore(MAX,'\n');
if(isConsole)
cout<<"Enter Salary:";
while(!(AnyStream>>Salary))
{
AnyStream.clear();
AnyStream.ignore(MAX,'\n');
}
AnyStream.ignore(MAX,'\n');
}
void CWorker::PrintOn(ostream& AnyStream)
{
CPerson::PrintOn(AnyStream);
bool isConsole=(AnyStream==cout);
if(isConsole)
AnyStream<<"\nTotalWeeklyWorkingHours:";
AnyStream<<TotalWeeklyWorkingHours;
if(isConsole)
AnyStream<<"\nSalary:";
AnyStream<<Salary;
}

class CTeacher:virtual public CPerson
{
protected:
char Specialization[MAX];
unsigned int SpecializationDegree;
void PrintOn(ostream& AnyStream);
void GetOn(istream& AnyStream);
public:
CTeacher():CPerson(),SpecializationDegree(0)
{
ObjectType=ClassTypeOfObject(this);
for(register int cntr1=0;cntr1<MAX;++cntr1)
*(this->Specialization+cntr1)=NULL;
}
virtual ~CTeacher() {}
};
void CTeacher::PrintOn(ostream& AnyStream)
{
CPerson::PrintOn(AnyStream);
bool isConsole=(AnyStream==cout);
if(isConsole)
AnyStream<<"Specialization:";
AnyStream<<Specialization;
if(isConsole)
AnyStream<<"SpecializationDegree:";
AnyStream<<SpecializationDegree;
}
void CTeacher::GetOn(istream& AnyStream)
{
CPerson::GetOn(AnyStream);
bool isConsole=(AnyStream==cin);
if(isConsole)
cout<<"Enter Specialization: (Ex: Alchemy)";
while(!(AnyStream>>Specialization))
{
AnyStream.clear();
AnyStream.ignore(MAX,'\n');
}
AnyStream.ignore(MAX,'\n');
if(isConsole)
cout<<"Enter Specialization Degree:";
while(!(AnyStream>>SpecializationDegree))
{
AnyStream.clear();
AnyStream.ignore(MAX,'\n');
}
AnyStream.ignore(MAX,'\n');
}
/* Dwarf is a TwoHandweaponmaster & their length can^'t be taller than 1.60 meter */
class CDwarf:virtual public CPerson
{
protected:
void GetOn(istream& AnyStream);
void PrintOn(ostream& AnyStream);
unsigned int TwoHandWeaponMastery;
public:
CDwarf():CPerson(),TwoHandWeaponMastery(0)
{
ObjectType=ClassTypeOfObject(this);
}
};
void CDwarf::GetOn(istream& AnyStream)
{
CPerson::GetOn(AnyStream);
bool isConsole=(AnyStream==cin);
if(isConsole)
cout<<"Enter TwoWeaponMastery:";
while(!(AnyStream>>TwoHandWeaponMastery))
{
AnyStream.clear();
AnyStream.ignore(MAX,'\n');
}
AnyStream.ignore(MAX,'\n');
}
void CDwarf::PrintOn(ostream& AnyStream)
{
CPerson::PrintOn(AnyStream);
bool isConsole=(AnyStream==cout);
if(isConsole)
cout<<"\nTwoHandWeaponMastery:";
AnyStream<<TwoHandWeaponMastery;
}
/* class CWizard haz its own abilities like FireMagic,WaterMagic,EarthMagic,AirMagic */
class CWizard:virtual public CPerson
{
protected:
unsigned int FireMagicDegree;
unsigned int WaterMagicDegree;
unsigned int EarthMagicDegree;
unsigned int AirMagicDegree;
unsigned int MaxMana;
unsigned int CurrentMana;
void PrintOn(ostream& AnyStream);
void GetOn(istream& AnyStream);
public:
CWizard():CPerson(),FireMagicDegree(0),WaterMagicDegree(0),EarthMagicDegree(0),
AirMagicDegree(0),MaxMana(0),CurrentMana(0)
{
ObjectType=ClassTypeOfObject(this);
}
virtual ~CWizard() {}
};
void CWizard::PrintOn(ostream& AnyStream)
{
CPerson::PrintOn(AnyStream);
bool isConsole=(AnyStream==cout);
if(isConsole)
AnyStream<<"\nFireMagicDegree:";
AnyStream<<FireMagicDegree;
if(isConsole)
AnyStream<<"\nWaterMagicDegree:";
AnyStream<<WaterMagicDegree;
if(isConsole)
AnyStream<<"\nEarthMagicDegree:";
AnyStream<<EarthMagicDegree;
if(isConsole)
AnyStream<<"\nAirMagicDegree:";
AnyStream<<AirMagicDegree;
if(isConsole)
AnyStream<<"\nMaxMana:";
AnyStream<<MaxMana;
if(isConsole)
AnyStream<<"\nCurrentMana:";
AnyStream<<CurrentMana;
}
void CWizard::GetOn(istream& AnyStream)
{
CPerson::GetOn(AnyStream);
bool isConsole=(AnyStream==cin);
if(isConsole)
cout<<"Enter FireMagicDegre:";
while(!(AnyStream>>FireMagicDegree))
{
AnyStream.clear();
AnyStream.ignore(MAX,'\n');
}
AnyStream.ignore(MAX,'\n');
if(isConsole)
cout<<"Enter WaterMagicDegre:";
while(!(AnyStream>>WaterMagicDegree))
{
AnyStream.clear();
AnyStream.ignore(MAX,'\n');
}
AnyStream.ignore(MAX,'\n');
if(isConsole)
cout<<"Enter EarthMagicDegre:";
while(!(AnyStream>>EarthMagicDegree))
{
AnyStream.clear();
AnyStream.ignore(MAX,'\n');
}
AnyStream.ignore(MAX,'\n');
if(isConsole)
cout<<"Enter AirMagicDegre:";
while(!(AnyStream>>AirMagicDegree))
{
AnyStream.clear();
AnyStream.ignore(MAX,'\n');
}
AnyStream.ignore(MAX,'\n');
if(isConsole)
cout<<"Enter MaxMana:";
while(!(AnyStream>>MaxMana))
{
AnyStream.clear();
AnyStream.ignore(MAX,'\n');
}
AnyStream.ignore(MAX,'\n');
if(isConsole)
cout<<"CurrentMana:";
while(!(AnyStream>>CurrentMana) || (CurrentMana>MaxMana))
{
if(isConsole)
cout<<"Curent Mana can't be bigger than MaxMana & You Must type as an integer Ex:20\n";
AnyStream.clear();
AnyStream.ignore(MAX,'\n');
}
AnyStream.ignore(MAX,'\n');
}
/* CKnight has TwoHandWeaponMastery and OneHandWeaoponMastery */
class CKnight:virtual public CPerson
{
protected:
unsigned int OneHandWeaponMastery;
unsigned int TwoHandWeaponMastery;
void GetOn(istream& AnyStream);
void PrintOn(ostream& AnyStream);
public:
CKnight():CPerson(),OneHandWeaponMastery(0),TwoHandWeaponMastery(0)
{
ObjectType=ClassTypeOfObject(this);
}
virtual ~CKnight() { }
};
void CKnight::GetOn(istream& AnyStream)
{
CPerson::GetOn(AnyStream);
bool isConsole=(AnyStream==cin);
if(isConsole)
cout<<"Enter OneHandWeaponMastery:";
while(!(AnyStream>>OneHandWeaponMastery))
{
AnyStream.clear();
AnyStream.ignore(MAX,'\n');
}
AnyStream.ignore(MAX,'\n');
if(isConsole)
cout<<"Enter TwoHandWeaponMastery:";
while(!(AnyStream>>TwoHandWeaponMastery))
{
AnyStream.clear();
AnyStream.ignore(MAX,'\n');
}
AnyStream.ignore(MAX,'\n');
}
void CKnight::PrintOn(ostream& AnyStream)
{
CPerson::PrintOn(AnyStream);
bool isConsole=(AnyStream==cout);
if(isConsole)
cout<<"\nOneHandWeaponMastery:";
AnyStream<<OneHandWeaponMastery;
if(isConsole)
cout<<"\nTwoHandWeaponMastery";
AnyStream<<TwoHandWeaponMastery;
}
/////////////////////////////////MAIN FUNCTION////////////////////////////////////////////////
int main(int argc,char* argv[])
{
CPerson* AnyOne;

AnyOne=new CWizard;
cin>>*AnyOne;
cout<<*AnyOne;
delete AnyOne;
AnyOne=new CKnight;
cin>>*AnyOne;
cout<<*AnyOne;
delete AnyOne;
return 0;
}

Share this post


Link to post
Share on other sites
It would help if you posted the exact error given by your compiler, but I'm guessing your problem is because you try to instantiate a CStudent and other derived classes before you're defined them. C++ has a single pass compilation model. How can the compiler know what constructors a class has and what it derives from before you've told it?
Wrong:

class CPerson;
class CStudent;

class CPerson
{
// declarations
};

CPerson * someFunc()
{
if (someCondition)
{
return new CPerson;
}
else
{
return new CStudent; // error - what is a CStudent?
}
}

class CStudent // too late - we needed this earlier
:
public CPerson
{
// declarations
};



Right:

class CPerson;
class CStudent;

class CPerson
{
// declarations
};

class CStudent
:
public CPerson
{
// declarations
};

CPerson * someFunc()
{
if (someCondition)
{
return new CPerson;
}
else
{
return new CStudent; // fine - we know what a CStudent is now
}
}



Btw:
char Name[MAX];

would be better (much better) as:
std::string Name;


Enigma

Share this post


Link to post
Share on other sites

This topic is 4689 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this