Hi,
I am trying to do the following:
1) Call a function
2) Ask the user for the datamember values
3) Create the object
4) Return to main() a pointer to the object
5) Push that pointer into the std::list
But I'm having trouble.
Here is what I've made:
base.h:
Code:
#include <iostream>
#include <string>
using namespace std;
class Base
{
public:
//Member Functions:
Base * createABaseObject();//Create an object and return a pointer to it
private:
protected:
//Data Members common to all objects (abstract base class):
string _mystring;
};
base.cpp:
Code:
#pragma once
#include <iostream>
#include <string>
#include "base.h"
Base * Base::createABaseObject()
{
//Build an object and return a pointer to the object.
//Then within main(), the pointer can be pushed into the std:list
cout << "You chose to create an object" << endl;
string mystring;
cout << "Value? " << endl;
getline (cin, mystring);
//create the object
Base *ptr = new Aircraft(mystring);
//return a pointer to main()
return(ptr);
}
//constructor
Base::Base(string mystring)
{
_mystring = mystring;
}
main.cpp:
Code:
#include <iostream>
#include <list>
#include <string>
#include "base.h"
using namespace std;
int main()
{
typedef list<Base *>BaseList;
do{
char choice;
cout << "Choose one of the options below:" << endl;
cout << "a) Enter a base class pointer into the list" << endl;
cin >> choice;
cin.ignore();
switch(choice)
{
case 'a':
{
//Create an object and return a pointer to the object
createABaseObject();
//push the returned pointer onto the list
BaseList.push_back( ptr );
cout << "Object pushed onto list" << endl;
break;
}
default:
{
cout << "You made an invalid choice" << endl;
break;
}
}
}
while(true);
system("pause");
}
This isn't compiling, and was hoping someone could comment on where I'm going wrong.
Any help is really appreciated.
Thanks