나를 괴롭혀, C++을 처음 접했습니다. 나는 벡터에 저장된 값을 업데이트하기 위해 노력하고있어,하지만 난이 오류 받고 있어요 : 나는 그래서 비슷한 contains
및 다른 사람과 같은 방법을 (공유 할 수 std::vector
주위에 간단한 래퍼를 사용하고중첩 클래스 C++에서 값을 참조하고 업데이트해야합니다.
non-const lvalue reference to type 'Node'
을 어떻게 ArrayList가 자바에 있는지). 다음과 같이
#include <vector>
using namespace std;
template <class T> class NewFrames {
public:
// truncated ...
bool contains(T data) {
for(int i = 0; i < this->vec->size(); i++) {
if(this->vec->at(i) == data) {
return true;
}
}
return false;
}
int indexOf(T data) {
for(int i = 0; i < this->vec->size(); i++) {
if(this->vec->at(i) == data) {
return i;
}
}
return -1;
}
T get(int index) {
if(index > this->vec->size()) {
throw std::out_of_range("Cannot get index that exceeds the capacity");
}
return this->vec->at(index);
}
private:
vector<T> *vec;
};
#endif // A2_NEWFRAMES_H
이 래퍼를 활용 클래스는 정의 :
#include "Page.h"
#include "NewFrames.h"
class Algo {
private:
typedef struct Node {
unsigned reference:1;
int data;
unsigned long _time;
Node() { }
Node(int data) {
this->data = data;
this->reference = 0;
this->_time = (unsigned long) time(NULL);
}
} Node;
unsigned _faults;
Page page;
NewFrames<Node> *frames;
};
내가 벡터의 내부 Node
개체 중 하나를 참조해야 할 점에있어,하지만 난 필요 reference
을 다른 값으로 변경할 수 있습니다. 다만 사용하여 내가 해봤
const Node &n = this->frames->get(this->frames->indexOf(data));
을 : 내가 SO에서 발견 한 바로는, 나는이 작업을 수행 할 필요가
Node n = this->frames->get(this->frames->indexOf(data));
n.reference = 1;
을 다음 디버거에서 데이터를 볼 수 있지만 값이 아닌 나중에 확인하면 업데이트됩니다. 이것을 고려해보십시오 :
const int data = this->page.pages[i];
const bool contains = this->frames->contains(Node(data));
Node node = this->frames->get(index);
for(unsigned i = 0; i < this->page.pages.size(); i++) {
if(node == NULL && !contains) {
// add node
} else if(contains) {
Node n = this->frames->get(this->frames->indexOf(data));
if(n.reference == 0) {
n.reference = 1;
} else {
n.reference = 0;
}
} else {
// do other stuff
}
}
루프의 후속 통과과 함께, 특정 데이터 값을 가진 노드는 다소 차이가납니다.
그러나 n.reference
을 변경하려고 시도하면 const
이 개체가 변경되지 않기 때문에 오류가 발생합니다. 이 노드를 변경하여 변경할 수있는 방법이 있습니까? 이런 일이 일어날 수있는 친숙한 자바 세상에서 나옵니다. 그러나이 이 C++에서이 아닌 이유를 알고 싶거나 이해하고 싶습니다.
이것은 문제가되지 않지만 두 개의 연속 된 밑줄 ('__A2_NEWFRAMES_H__')과 밑줄과 대문자로 시작하는 이름은 구현에 예약되어 있습니다. 사용하지 마십시오. –
불필요한 수동 메모리 관리를 피하기 위해'벡터 * vec;'를'벡터 vec;'로 대체해야합니다. –
nwp
Sigh. 문제를 보여주는 코드를 만들 때까지 코드를 자릅니다. 무엇이 잘못되었는지를 알아 내기 위해 이처럼 관련성이없는 모든 것들을 헤쳐 나갈 사람은 아무도 없을 것입니다. –