I want to create a vector of objects, most likely derived from a base class, which might or might not have their own personal non-derived functions. As of yet I am unable to call these functions as they are not part of the class used in the declaration of the vector.
How would one go about on making this possible?
Code below gives the error:
../src/ObjectVectors.cpp:22:33: error: no member named 'getInt' in 'BaseObject'
//============================================================================
// Name : ObjectVectors.cpp
// Author : Edwin Rietmeijer
// Version :
// Copyright : This code is owned by Edwin Rietmeijer as of 2014
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <vector>
#include "BaseObject.h"
#include "SubObjA.h"
using namespace std;
int main() {
vector<BaseObject *> objectVector;
objectVector.push_back( new SubObjA );
cout << objectVector.front() -> getInt() << endl;
// for ( pos = objectVector.begin(); pos != objectVector.end(); ++pos )
// cout << pos -> get() << endl;
}
/*
* BaseObject.h
*
* Created on: Feb 11, 2014
* Author: edwinrietmeijer
*/
#ifndef BASEOBJECT_H_
#define BASEOBJECT_H_
class BaseObject {
public:
BaseObject();
int get();
virtual ~BaseObject();
};
#endif /* BASEOBJECT_H_ */
/*
* BaseObject.cpp
*
* Created on: Feb 11, 2014
* Author: edwinrietmeijer
*/
#include "BaseObject.h"
BaseObject::BaseObject() {
// TODO Auto-generated constructor stub
}
int BaseObject::get(){
return 0;
}
BaseObject::~BaseObject() {
// TODO Auto-generated destructor stub
}
/*
* SubObjA.h
*
* Created on: Feb 11, 2014
* Author: edwinrietmeijer
*/
#ifndef SUBOBJA_H_
#define SUBOBJA_H_
#include "BaseObject.h"
class SubObjA : public BaseObject {
int data = 88;
public:
SubObjA();
int getInt();
virtual ~SubObjA();
};
#endif /* SUBOBJA_H_ */
/*
* SubObjA.cpp
*
* Created on: Feb 11, 2014
* Author: edwinrietmeijer
*/
#include "SubObjA.h"
SubObjA::SubObjA() {
// TODO Auto-generated constructor stub
}
int SubObjA::getInt() {
return data;
}
SubObjA::~SubObjA() {
// TODO Auto-generated destructor stub
}