I have several problems with a code which has a similar structure to this one shown. It doesn't work! I think the main problem is the push_back function, that I need for other scopes (I need to build a vector of classes during the operations, and also have pointers internally the single class). Anyone knows how to solve it? Thanks
#include <vector>
using namespace std;
class meow {
public:
int b;
meow() {
b = 1;
}
meow(int a) {
b = a;
}
};
class dog {
public:
vector<meow> H;
vector<vector<meow>::iterator> HP;
dog()
: HP(2), H(2) {
HP[0] = H.begin();
HP[1] = H.begin() + 1;
}
};
int main() {
vector<dog> WOOF(1);
WOOF.push_back(dog());
meow ok(2);
(*WOOF[1].HP[0]) = ok;
cout << (*WOOF[0].HP[0]).b << endl;
cout << (*WOOF[1].HP[1]).b << endl;
}
OK, I understood the deal, so is it possible to do something like this, in the event that I won't use push_back after in the code?
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
class meow {
public:
int b;
meow() {
b = 1;
}
meow(int a) {
b = a;
}
};
class dog{
public:
vector <meow> H;
vector<vector<meow>::iterator> HP;
dog():
HP(2),H(2){
}
void Referring(){
HP[0]=H.begin();
HP[1]=H.begin()+1;
}
};
int main() {
vector<dog> WOOF(1);
WOOF.push_back(dog());
for(int i=0;i<WOOF.size();++i){
WOOF[i].Referring();
}
meow ok(2);
(*WOOF[1].HP[0]) = ok;
cout << (*WOOF[0].HP[0]).b << endl;
cout << (*WOOF[1].HP[1]).b << endl;
}