I had this weird thing while I was trying to avoid pointers and just create objects on the stack.
#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <cstdio>
using namespace std;
class Node {
public:
string move;
int state[10];
Node(const Node &p) {
move = "";
memcpy(state,p.state,sizeof(int)*10);
}
Node(){
move = "";
for(int i = 0;i<10;i++){
state[i] = i;
}
}
void AddNode(Node p){
this->childNode.push_back(p);
}
Node GetChild(int i) {
return childNode[i];
}
int GetCount() {
return this->childNode.size();
}
protected:
vector<Node> childNode;
};
class Move {
public:
Move() {
}
void run() {
Node n;
n.move = "A100";
operate(n);
}
void operate(Node &n) {
Node c(n);
c.move = "B100";
n.AddNode(c);
cout << "Output : " << endl;
Node t = n.GetChild(n.GetCount() - 1);
//Node t = n.childNode[0];
for(int i = 0;i<10;i++)
cout << t.state[i];
cout << endl << t.move; // doesn't display B100 ????
}
};
int main(int argc,char** argv) {
Move m;
m.run();
return 0;
}
#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <cstdio>
using namespace std;
class Node {
public:
string move;
int state[10];
Node(const Node &p) {
move = "";
memcpy(state,p.state,sizeof(int)*10);
}
Node(){
move = "";
for(int i = 0;i<10;i++){
state[i] = i;
}
}
void AddNode(Node p){
this->childNode.push_back(p);
}
Node GetChild(int i) {
return childNode[i];
}
int GetCount() {
return this->childNode.size();
}
protected:
vector<Node> childNode;
};
class Move {
public:
Move() {
}
void run() {
Node n;
n.move = "A100";
operate(n);
}
void operate(Node &n) {
Node c(n);
c.move = "B100";
n.AddNode(c);
cout << "Output : " << endl;
Node t = n.GetChild(n.GetCount() - 1);
//Node t = n.childNode[0];
for(int i = 0;i<10;i++)
cout << t.state[i];
cout << endl << t.move; // doesn't display B100 ????
}
};
int main(int argc,char** argv) {
Move m;
m.run();
return 0;
}
To copy to clipboard, switch view to plain text mode
Here is a sample of something that I'm trying to do. My question is why isn't B100 move displayed ? Do I need an assignment operator in the Node class ? I believed push_back takes references and that the node with move value B100 should be stored in the vector. Correct state array is stored but the move string is not, any suggestions ?
Bookmarks