这就是问题所在:我有一个名为“state”的类,它存储了一个 int 向量。在我的程序中,我正在创建一个状态向量,但之后当我尝试获取 int 向量的值时,它似乎只是空的。我创建了一个 .cpp 文件来测试它:
状态.h:
#ifndef STATE_H
#define STATE_H
#include <vector>
class state {
public:
std::vector<int> vectorstate;
~state();
state();
state(state &sta);
state(const state &sta);
state & operator= (const state &sta);
bool operator==(const state &sta) const;
};
#endif
状态.cpp:
#include "state.h"
using namespace std;
state::~state(){
}
state::state(){
}
state::state(state &est){
for(int i=0;i<vectorstate.size();i++)
vectorstate[i] = est.vectorstate[i];
}
state::state(const state &est){
for(int i=0;i<vectorstate.size();i++)
vectorstate[i] = est.vectorstate[i];
}
state & state::operator= (const state &est) {
if (this == &est) return *this;
for(int i=0;i<vectorstate.size();i++)
vectorstate[i] = est.vectorstate[i];
return *this;
}
bool state::operator==(const state &est) const {
for(int i=0;i<vectorstate.size();i++)
if(vectorstate[i] != est.vectorstate[i])
return false;
return true;
}
main.cpp(用于测试目的):
#include <iostream>
#include <algorithm>
#include <vector>
#include "state.h"
using namespace std;
int main(){
state before, after;
for(int i=0;i<9;i++)
before.vectorstate.push_back(i);
for(int i=0;i<before.vectorstate.size();i++)
cout << before.vectorstate[i] << " ";
cout << endl << endl;
for(int i=3;i<12;i++)
after.vectorstate.push_back(i);
for(int i=0;i<after.vectorstate.size();i++)
cout << after.vectorstate[i] << " ";
cout << endl << endl;
vector<state> vetor;
vetor.push_back(before);
vetor.push_back(after);
for(int i=0;i<vetor[0].vectorstate.size();i++)
cout << vetor[0].vectorstate[i] << " ";
cout << endl;
for(int i=0;i<vetor[1].vectorstate.size();i++)
cout << vetor[1].vectorstate[i] << " ";
cout << endl;
state aux;
aux = vetor.front();
for(int i=0;i<aux.vectorstate.size();i++)
cout << aux.vectorstate[i] << " ";
return 0;
}
这个输出:
http://i.imgur.com/A60K72e.jpg
(对不起链接,我不能在这里发布图片,我猜是声誉的原因)。
如您所见,我尝试了不同的方式来访问这个 int 向量,但它没有给我任何结果,任何帮助将不胜感激!