English 中文(简体)
顺差 c ++
原标题:passing current object c++

I want to pass a pointer of the current object to a newly created object of the same type. I tried it like this, but somehow the new object prev field points to itself. I m using the Microsoft Visual C++ compiler.

Class A{

    A *prev;

    A(A* a)
    {
        prev = a;
    }
    vector<A> addToVector()
    {
        vector<A> res;
        res.push_back(A(this));
        return res;
    }

};

现在,从病媒中得出结果,并补充说,在问题中,点人指自己,而不是其前任。 什么是错的?

页: 1

在这里,这部法律将新国家添加到病媒中,并送回它们。

const int dim = 3;

int goal[] = {1,2,3,4,5,6,7,8,0};

class State{
public:
vector<int> board;
const State *prev;
int g;
int h() const{
    int res = 0;
    for(int i=0; i<dim*dim; i++){
        if(goal[i] != board[i]) res++;
        }
    return res;
    }
inline bool operator==(const State &other) const{
    bool eq = true;
    for(int i=0; eq && i<dim*dim; i++){
        eq = board[i] == other.board[i];
        }
    return eq;
    }

inline bool operator<(const State& other) const{
    return g + h() > other.g + other.h();
    }
inline State& operator=(const State& other){
    this->board = other.board;
    this->g = other.g;
    this->prev = other.prev;
    assert(this != prev);
    return *this;
    }
State(int a[], int b, const State *p){
    board.assign(a, a+dim*dim);
    g = b;
    prev = p;
    assert(prev != this);
    }
bool isSolution(){
    bool isSol = true;
    for(int i=0; i<dim*dim && isSol; i++){
        isSol = board[i] == goal[i];
        }
    return isSol;
}

vector<State> getSuccessors(){
    vector<State> successors;
    // find zero
    bool found = false;
    int z_pos;
    for(int i=0; i<dim*dim && !found; i++){
        found = board[i] == 0;
        if(found) z_pos = i; 
        }
    switch(z_pos){
    case 0:
        {
        // 1st row left
        int n1[] = {board[1], 0, board[2], board[3],board[4],board[5],board[6],board[7],board[8]};
        // 1st columnn up
        int n2[] = {board[3], board[1], board[2], 0,board[4],board[5],board[6],board[7],board[8]};
        State s1(n1, g+1, this);
        State s2(n2, g+1, this);
        successors.push_back(s1);
        successors.push_back(s2);
        } break;
    case 1:
        {
        // 1st row left
        int n1[] = {board[0], board[2], 0, board[3],board[4],board[5],board[6],board[7],board[8]};
        // 1st row right
        int n2[] = {0, board[0], board[2], board[3],board[4],board[5],board[6],board[7],board[8]};
        // 2nd column up
        int n3[] = {board[0], board[4], board[2], board[3],0,board[5],board[6],board[7],board[8]};
        State s1 (n1, g+1, this);
        State s2 (n2, g+1, this);
        State s3 (n3, g+1, this);
        successors.push_back(s1);
        successors.push_back(s2);
        successors.push_back(s3);
        } break;
    case 2:
        {
        // 1st row right
        int n1[] = {board[0], 0, board[1], board[3],board[4],board[5],board[6],board[7],board[8]};
        // 3rd column up
        int n2[] = {board[0], board[1], board[5], board[3],board[4],0,board[6],board[7],board[8]};
        State s1(n1, g+1, this);
        State s2(n2, g+1, this);
        successors.push_back(s1);
        successors.push_back(s2);
        } break;
    case 3:
        {
        // 1st column up
        int n1[] = {board[0], board[1], board[2], board[6],board[4],board[5],0,board[7],board[8]};
        // 1st column down
        int n2[] = {0, board[1], board[2], board[0],board[4],board[5],board[6],board[7],board[8]};
        // row 2 left
        int n3[] = {board[0], board[1], board[2], board[4],0,board[5],board[6],board[7],board[8]};
        State s1(n1, g+1, this);
        State s2(n2, g+1, this);
        State s3(n3, g+1, this);
        successors.push_back(s1);
        successors.push_back(s2);
        successors.push_back(s3);
        } break;
    case 4:
        {
        // row 2 right
        int n1[] = {board[0], board[1], board[2], 0,board[3],board[5],board[6],board[7],board[8]};
        // row 2 left
        int n2[] = {board[0], board[1], board[2], board[3],board[5],0,board[6],board[7],board[8]};
        // column 2 up
        int n3[] = {board[0], board[1], board[2], board[3],board[7],board[5],board[6],0,board[8]};
        // column 2 down
        int n4[] = {board[0], 0, board[2], board[3],board[1],board[5],board[6],board[7],board[8]};
        State s1(n1, g+1, this);
        State s2(n2, g+1, this);
        State s3(n3, g+1, this);
        State s4(n4, g+1, this);
        successors.push_back(s1);
        successors.push_back(s2);
        successors.push_back(s3);
        successors.push_back(s4);
        } break;
    case 5:
        {
        // row 2 right
        int n1[] = {board[0], board[1], board[2], board[3],0,board[4],board[6],board[7],board[8]};
        // column 3 up
        int n2[] = {board[0], board[1], board[2], board[3],board[4],board[8],board[6],board[7],0};
        // column 3 down
        int n3[] = {board[0], board[1], 0, board[3],board[4],board[2],board[6],board[7],board[8]};
        State s1(n1, g+1, this);
        State s2(n2, g+1, this);
        State s3(n3, g+1, this);
        successors.push_back(s1);
        successors.push_back(s2);
        successors.push_back(s3);
        } break;
    case 6:
        {
        // row 3 left
        int n1[] = {board[0], board[1], board[2], board[3],board[4],board[5],board[7],0,board[8]};
        // column 1 down
        int n2[] = {board[0], board[1], board[2], 0,board[4],board[5],board[3],board[7],board[8]};
        State s1(n1, g+1, this);
        State s2(n2, g+1, this);
        successors.push_back(s1);
        successors.push_back(s2);
        } break;
    case 7:
        {
        // row 3 right
        int n1[] = {board[0], board[1], board[2], board[3],board[4],board[5],0,board[6],board[8]};
        // row 3 left
        int n2[] = {board[0], board[1], board[2], board[3],board[4],board[5],board[6],board[8],0};
        // column 2 down
        int n3[] = {board[0], board[1], board[2], board[3],0,board[5],board[6],board[4],board[8]};
        State s1(n1, g+1, this);
        State s2(n2, g+1, this);
        State s3(n3, g+1, this);
        successors.push_back(s1);
        successors.push_back(s2);
        successors.push_back(s3);
        } break;
    case 8:
        {
        // row 3 right
        int n1[] = {board[0], board[1], board[2], board[3],board[4],board[5],board[6],0,board[7]};
        // column 3 down
        int n2[] = {board[0], board[1], board[2], board[3],board[4],0,board[6],board[7],board[5]};
        State s1(n1, g+1, this);
        State s2(n2, g+1, this);
        successors.push_back(s1);
        successors.push_back(s2);
        } break;
    }
    return successors;
}

void getPath(){
    assert(prev == this);
    cin.get();
    }

};


void solve(){
priority_queue<State> openSet;
set< vector<int> > closedSet;
int init[] = {1,0,3,4,2,6,7,5,8};
State initial(init,0,NULL);
openSet.push(initial);
while(!openSet.empty()){
    State n = openSet.top();
    assert(&n != n.prev); //FAILS
    openSet.pop();
    if(n.isSolution()){
        cout << "openSet size:   " << openSet.size() << endl;
        cout << "closedSet size: " << closedSet.size() << endl;
        n.getPath();
        break;
        }
    else if(closedSet.find(n.board) != closedSet.end()){
        ; // skip
        }
    else{
        closedSet.insert(n.board);
        vector<State> successors = n.getSuccessors();
        for(int i=0; i<successors.size(); i++){
            if(closedSet.find(successors[i].board) == closedSet.end()) openSet.push(successors[i]);
        }
    }
}
}

int main(){
//freopen("packrec.in", "r", stdin);
//freopen("packrec.out", "w", stdout);
//int t; cin >> t; while(t--)
    solve();
return 0;
}
最佳回答

如不阅读(长期)背景代码,则需要谨慎,不要在范围外的物体上打add ToVector。 由于必须直接创建当地类型的标的<代码>A(因为病媒可能超出其寿命),所有构造者<代码>A:A应当是非公开的(但影印机除外,该文本的标码<>/代码>必须查阅),因此物体应当从工厂职能中收回,而工厂职能中应归还。

如果你打算通过确定一个地方变量来制造一系列相关物体,那就很容易写上一席之地,但因为地方的地址在整座的每一座,所有物体似乎都是相同的。 这很容易造成自我联系。

Ah,我搜索了背景代码中的add ToVector,甚至根本没有使用。 因此,一般建议仍然有效。 这也是你ed熟地将守则按此方式加以推广。

问题回答

在你施工后,似乎不可能获得<代码>this=这一->prev。 然而,这只被搁置了很久。 我怀疑他们没有。 证明这一点的欧亚办法是维护<代码>prev:

A::A(A* p): prev(p) { assert(this != this->prev); }
A::A(A const& a): prev(a.prev) { assert(this != this->prev); }
A& A::operator= (A const& a) {
    this->prev = a.prev;
    assert(this != this->prev); 
}

我怀疑在某个时候被提及的最初物体已经消失,而且该地点被一个物体重新使用,而不用提。 你乐意通过物体,加上对这些物体的贴切点,可能成为候选人。 由于你不完整(而且大多无关紧要)的法典,这个问题可能会重现,因此,如果这个问题确实存在,我无法证实。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签