这是问题所在: 我有一个名为'state'的类,它存储一个int向量。在我的程序中,我正在创建一个状态向量,但在此之后,当我尝试获取int向量的值时,它似乎只是空的。我创建了一个.cpp文件来测试它:
state.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
state.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向量,但它没有给我任何结果,任何帮助都将非常感激!
答案 0 :(得分:2)
问题在于你的拷贝构造函数和赋值运算符的实现。
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;
}
如果vectorstate
的{{1}}当前为空,则this
将返回vectorstate.size()
,并且永远不会执行for循环的内容。在复制构造函数和赋值运算符中,请确保复制完整的向量(0
具有工作副本构造函数)。
或者更好的是,不要实现它们,让编译器生成的复制构造函数和赋值完成工作。
答案 1 :(得分:0)
我将您的代码重新设计为“小”并且瞧瞧:
#include <algorithm>
#include "state.h"
using namespace std;
state::state() {}
state::state(const state &est) : vectorstate(est.vectorstate) {}
state & state::operator= (const state &est) {
if (this == &est) return *this;
vectorstate.resize(est.vectorstate.size());
std::copy(est.vectorstate.begin(), est.vectorstate.end(), vectorstate.begin());
return *this;
}
bool state::operator==(const state &est) const { return est.vectorstate == vectorstate; }
#ifndef STATE_H
#define STATE_H
#include <vector>
struct state {
std::vector<int> vectorstate;
state();
state(const state &sta);
state & operator=(const state &sta);
bool operator==(const state &sta) const;
};
#endif
vectorstate
为空,因此无法按下标复制元素。==
运算符就足够了。