@laserLight
Thank you for the informations.
Btw, i need to access the vector created in the main from within a class:
This is the Main.cpp
Code:
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <string>
#include <vector>
#include "include/object.h"
#include "include/animal.h"
using namespace std;
vector<CObject> Obj;
CObject Key ("Collar", );
CObject Chain ("Chain", );
Cobject Bell ("Bell")
int Stable=0;
Animal sheep ("Snow", "Sheep", 1);
string myName=sheep.getName();
Animal dog ("Lassie", "dog", 1);
string dogName=dog.getName();
vector<Animal> animalList;
int main()
{
bool running=true;
//Obj and other vector must be initialized, first, then push the object
Obj.push_back(Key);
Obj.push_back(Gold);
animalList.push_back(sheep);
animalList.push_back(dog);
system("CLS");
cout << "The " sheep.type << " name is: " << sheep.getName << endl;
cout << "The " dog.type << " name is: " << dog.getName << endl;
return 0;
}
the animal.h class:
Code:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
extern vector<int> Obj; // doesn't work conflicting declaration 'std::vector<CObject> Obj'
/*here a would like to access the object created in main (vector<CObject> Obj;), iterate through it and modifying the values, adding or removing elements.
Many suggested to avoid using globals */
struct Object {
string name;
string type;
int status;
};
class Animal{
public:
string _name;
string _type;
int _inStable, _status=0;
vector<int> objects;
Animal (string, string, int);
string getName () {return (_name);}
void addObject(string objName);
void removeObject(string name);
};
void Animal::addObject(string ObjName) {
//to do
}
void Animal::removeObject(string name){
//to do
}
Animal::Animal(string name, string type, int status) {
_name = name;
_type = type;
_status = status;
}
The object class:
Code:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class CObject{
public:
string _name;
string _type;
int _status;
CObject (string, string, int);
string getName () {return (_name);}
void setName(string nm) { _name=nm;}
int getStatus() { return _status;}
void setStatus (int sts){
_status=sts;
}
};
CObject::CObject (string name, string type, int status) {
_name=name;
_type=type;
_status=status;
}