O.K., I have a project I'm working on. It uses 8 classes (I'm only showing 2 and main). It gives me a "bad_alloc" error when the obj is instantiated. When obj is removed it works fine (or call the personType class that "should" be inherited from extPersonType.) Any thoughts of what I am missing or doing wrong?

(class header - TO BE inherited)
using namespace std;
#ifndef PERSONTYPE_H
#define PERSONTYPE_H
class personType{

protected:
	string firstName;
	string lastName;
public:
    void print()const;
	void setName(string first, string last);
	string getFirstName()const;
	string getLastName()const;
	personType(string first = "", string last = "");
	/*personType();*/
};
#endif

( same class defined )

#include <string>
#include <iostream>
#include "personType.h"

using namespace std;


void personType::print()const{
	cout << endl;
	cout << firstName << " " << lastName << endl;
}
void personType::setName(string first, string last){

	firstName = first;
	lastName = last;
}
string personType::getFirstName()const{

	return firstName;
}
string personType::getLastName()const{

	return lastName;
}
personType::personType(string first,string last){

	firstName = first;
	lastName = last;
}
//personType::personType(){
//
//	firstName = "";
//	lastName = "";
//}

( class header to inherit personType)
using namespace std;


class extPersonType:public personType{

private:
	string relation;
	string phone;
public:
	void print()const;
	void setRelation(string phone, string extRelation);
	void setPhone(string extPhone);
	void setRelation(string extRelation);
	string getRelation()const;
	string getPhone()const;
	extPersonType(string relation = "",string phone = "");
	/*extPersonType();*/
};

(same class defined )
#include <string>
#include <iostream>
#include "personType.h"
#include "extPersonType.h"

//#include "dateType.h"
//#include "addressType.h"
using namespace std;


void extPersonType::print()const{

	cout << relation <<  " " << phone  << endl;
}
void extPersonType::setRelation(string extRelation){
	relation = extRelation;
}
void extPersonType::setPhone(string extPhone){
	phone = extPhone;
}
string extPersonType::getRelation()const{

	return relation;
}
string extPersonType::getPhone()const{

	return phone;
}
extPersonType::extPersonType(string extRelation, string extPhone)
:personType(firstName,lastName) {

	relation = extRelation;
	phone = extPhone;
}
////extPersonType::extPersonType():personType()/*:dateType():addressType()*/{ //maybe the same here :personType(...):addressType(...)
////
////	relation = "";
////	phone = "";
////}

(main() that calls extPersonType obj to pass values to make the personType class do it's inherited work. - bad_alloc error on compile )
#include <fstream>
#include <iostream>
#include <string>
#include "personType.h"
#include "addressType.h"
#include "extPersonType.h"
#include "dateType.h"
using namespace std;

int main(){

	//ofstream file;
	//file.open("MyTxT.txt");
	
	cout << "******************    Welcome to your address book.    ***********************\n" << endl;
		             
        string first,last;
	
	extPersonType myExt;
    //personType myPerson; --> for another class


			cout << "Enter the first name: ";
			getline(cin,myExt.firstName);
            cout << "Enter the last name: ";
			getline(cin,myExt.lastName);
			myExt.setName(first,last);
		

//cin.sync();  
//cin.peek();  

// 
};

any information, thoughts, help, ideas are greatly appreciated

'It gives me a "bad_alloc" error when the obj is instantiated.'...Which object?

Usually a "bad_alloc" is associated with a pointer, but I don't see any in your code.

My suspicions:

extPersonType myExt;

This declaration creates a "default" object. The problem is that you have no default constructors, you have "//commented them out". The system is not providing you a default constructor because you have defined overloaded constructors. As a result, those are the only constructors you have, and they don't match the calls.

Usually a "bad_alloc" is associated with a pointer, but I don't see any in your code.

My suspicions:

This declaration creates a "default" object. The problem is that you have no default constructors, you have "//commented them out". The system is not providing you a default constructor because you have defined overloaded constructors. As a result, those are the only constructors you have, and they don't match the calls.

when I un-comment the default constructors , I get a "extPersonType, and personType ambiguous call to overloaded function" I thought they were setup correctly. (and it's to the myExt obj.) If I take out the myExt obj and just go directly to getline(cin, personType.lastName) it works. But that completely goes against using inheritance...I'm stuck.

Hmmm...... I'm still looking, but in the mean time, please answer me these:

What IDE/Compiler are you using?

Are you getting the "bad_alloc" at runtime or compile time?

The problem is in the extPersonType() constructor where you pass the uninitialized member variables firstName and lastName to the base class constructor, i.e. trying to initialize uninitialized variables with copies of themselves.

Since the base class constructor has default parameters, the fix is easy, i.e.

extPersonType::extPersonType(string extRelation, string extPhone)
: personType(/* using the default arguments */)
{
	relation = extRelation;
	phone = extPhone;
}

In general,
-- to avoid making unnecessary copies of function arguments, pass by const reference
-- use initialization lists instead of assignment

So this constructor could turn into ..

extPersonType::extPersonType(const string & extRelation, const string & extPhone)
:	personType(/* using the default arguments */)
, 	relation(extRelation)
, 	phone(extPhone)
{
  // Nothing to do in the body
}

Hmmm...... I'm still looking, but in the mean time, please answer me these:

What IDE/Compiler are you using?

Are you getting the "bad_alloc" at runtime or compile time?

I am using Visual Studio 2008. and getting bad_alloc" at compile time. I'm thinking it's in how extPersonType is/in not inheriting personType correctly/fully. If I drop myExt obj and do getline(cin, first) and geline(cin, last) it seems to somewhat work. Although personType.print(); or personType::print(); is not working. (((sigh))).

You might apply the changes described above to the extPersonType() constructor, and then reduce your main() to the following, just to make sure that the constructors are working as intended.

#include <fstream>
#include <iostream>
#include <string>
#include "personType.h"
#include "addressType.h"
#include "extPersonType.h"
#include "dateType.h"
using namespace std;

int main()
{
  try
  {
    // Try to instantiate an extPersonType
    extPersonType myExt;
  }
  catch(const exception & e)
  {
    cout << "exception: " << e.what() << endl;
  }
  catch(...)
  {
    cout << "Unknown exception." << endl;
  }
}

You might apply the changes described above to the extPersonType() constructor, and then reduce your main() to the following, just to make sure that the constructors are working as intended.

#include <fstream>
#include <iostream>
#include <string>
#include "personType.h"
#include "addressType.h"
#include "extPersonType.h"
#include "dateType.h"
using namespace std;

int main()
{
  try
  {
    // Try to instantiate an extPersonType
    extPersonType myExt;
  }
  catch(const exception & e)
  {
    cout << "exception: " << e.what() << endl;
  }
  catch(...)
  {
    cout << "Unknown exception." << endl;
  }
}

With mitrmkar's suggestion and both the try/catch suggestion, I'm getting a "ambiguous call to overloaded function" from the extPersonType myExt object. Don't see how it's "ambiguous". I'll fight with it more an update results. And THANK YOU very much for the help and input guys! your time is greatly appreciated.

>> I'm getting a "ambiguous call to overloaded function" from the extPersonType myExt

I think you may have been trying to write two default constructors which you cannot do, perhaps see http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.4.

The following is what I suggested you could do in order to make the constructors work properly ..

#include <iostream>
#include <string>
using namespace std;

struct A
{
  string a, b;

  // A default constructor
  A(const string & a_ = "a", const string & b_ = "b")
  : a(a_)
  , b(b_)
  {
    cout << "A() a: " << a << "\tb: " << b << endl;
  }

  //// Since a default constructor already exists,
  //// you cannot have the following constructor
  // A() : a(""), a("") {}
};

struct B : A
{
  string c, d;

  // A default constructor
  B(const string & c_ = "c", const string & d_ = "d")
  : A()
  , c(c_)
  , d(d_)
  {
    cout << "B() c: " << c << "\td: " << d << endl;
  }

  //// Since a default constructor already exists,
  //// you cannot have the following constructor
  // B() : c(""), d("") {}
};

int main()
{
  A a;
  B b;
}

Something is strange here. I'm also using the VS 2008 IDE/Compiler so I should be experiencing similar behavior. I had to comment the #includes for your addressType and dateType because I don't have them. When I did, this is all that I got:

main.cpp(24) : error C2248: 'personType::firstName' : cannot access protected member declared in class 'personType'
persontype.h(7) : see declaration of 'personType::firstName'
persontype.h(4) : see declaration of 'personType'
main.cpp(26) : error C2248: 'personType::lastName' : cannot access protected member declared in class 'personType'
persontype.h(8) : see declaration of 'personType::lastName'
persontype.h(4) : see declaration of 'personType'
implementationFile2.cpp

Notice that it's flagging lines 24 and 26. In my copy of your file those lines are:

cout << "Enter the first name: ";
getline(cin,myExt.firstName);     //<---Line 24
cout << "Enter the last name: ";
getline(cin,myExt.lastName);
myExt.setName(first,last);

Since this is not the same error you are getting, it leads me to suspect that the issue you are posting about is in either your addressType files or your dateType files.

Like I said earlier, bad_alloc is an exception that is usually thrown at run time when using dynamic memory and pointers, but I don't see any of that in the code you posted. I think you need to try to re-compile and do an actual copy/paste of the first error returned by the compiler, not just give us a summary. Then, post the exact contents of the file flagged as well.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.