problrm with array in c++

problrm with array in c++

本文关键字:c++ in array with problrm      更新时间:2023-10-16

这是我的Stata.h

  #include "State.h"
#include <iostream>  
using namespace std;
    class State
    {
    public:
        int data[4][4];
        int x;
        int y;
        int cost[];
        State(void);
        void CopyState(State*);
        bool equals(State*);
        int h(State*);
        void print(void);
    private:
        int getLocation(State, int, int);
    };

这是我的国家.cpp

#include "State.h"
#include <iostream>
using namespace std;

State::State(void)
{
    State::cost[]  = {0, 3,  2,  5,  3,  4,  1,  2,  2,  1,  4,  3,
                 5, 2,  3,  2,  3,  0,  3,  2,  2,  3,  2,  1,  
                 1, 2,  1,  4,  2,  3,  2,  3,  2,  3,  0,  3, 
                 1, 2,  3,  2,  4,  1,  2,  1,  3,  2,  3,  2,  
                 5, 2,  3  ,0  ,2 , 1 , 4 , 3  ,3 , 4  ,1 , 2,  
                 2, 3,  2,  5,  3,  2,  1,  2,  0,  3,  2,  3,  
                 3,  2, 1  ,2  ,2 , 1  ,4  ,3 , 4  ,3  ,2 , 1 ,
                 3  ,0, 3,  2,  2 , 3,  2,  1 , 1 , 2,  1,  4, 
                 1 , 2, 3  ,4  ,2  ,3  ,0  ,3  ,1 , 2  ,3  ,2 ,
                 4  ,1, 2,  1,  2,  1,  2,  3,  3,  2,  3,  0, 
                 2  ,1, 2 , 3  ,3  ,4  ,1  ,2 , 2 , 1  ,4  ,3, 
                 3 , 2, 1,  2,  0 , 3 , 2 , 3,  3 , 2,  1,  2, 
                 1  ,2, 1 , 4  ,2  ,3  ,2 , 1 , 3 , 0 , 3 , 2,
                 4 , 3, 2 , 1 , 4 , 1,  2 , 1 , 1 , 2,  3 , 2, 
                 2,  3 ,0  ,3  ,1 , 2 , 3 , 4 , 3  ,4 , 1  ,2,
                 2  ,1, 2,  3 , 3 , 2 , 3 , 0,  2,  1,  2,  3,
                 5 , 2  ,3 , 2  ,2  ,1 , 4  ,3  ,3  ,4  ,1  ,2, 
                 0  ,3,  2,  5,  2 , 3 , 2,  3,  1,  2,  1,  4,
                 2  ,3  ,2 , 1  ,3 , 0  ,3 , 2,  3 , 2  ,3  ,2 ,
                 4  ,1,  2,  1,  1 , 2,  3 , 2 , 2,  3 , 0,  3, 
                 2  ,3  ,2 , 5 , 3 , 4 , 1 , 2 , 2 , 1  ,4 , 3 , 
                 5 , 2  ,3  ,0  
               };
}
void State::CopyState(State *state)
{
    for (int i = 0; i < 4; i++)
            for (int j = 0; j < 4; j++)
                data[i][j] = state->data[i][j];
        x = state->x;
        y = state->y;
}
bool State::equals(State *state)
{
    for (int i = 0; i < 4; i++)
         for (int j = 0; j < 4; j++)
              if (data[i][j] != state->data[i][j])
                return false;
    return true;
}
int State::h(State *state)
{
    int k = 0;
    for (int i = 0; i < 4; i++)
        for (int j = 0; j < 4; j++)
        {
            int frmPos = 4 * i + j;
            int toPos = getLocation(*state, i, j);
            k += cost[16 * frmPos + toPos];
        }
    return k;
}
void State::print()
{
    cout << "n";
    for (int i = 0; i < 4; i++)
    {
        cout << "n";
        for (int j = 0; j < 4; j++)
            cout << data[i][j];
    }
    cout << endl;    
}

问题出在状态中.cpp当我尝试为成本数组赋值时在构造函数中,我不知道问题出在哪里,请任何人都可以解决这个问题,对不起我的英语不好

你不能

那样做。 有几个问题,但第一个问题是:

在标头中,您将cost声明为具有未指定大小的数组:

int cost[];

这在标准C++中是不允许的,因为现在编译器不知道数组有多大,因此不知道State会有多大。(某些编译器确实允许它,但它是一个特殊的扩展,不适用于其他编译器。

执行此操作的最简单通常是最好的方法是使用vector

#include <vector>
class State
{
  // ...
  std::vector <int> cost;
};

然后在构造函数中:

State::State()
{
  static const int startingValues [] =
    {0, 3,  2,  5,  3,  4,  1,  2,  2,  1,  4,  3, ... };
  static const size_t numStartingValues = sizeof (startingValues) / sizeof (startingValues [0]);
  std::copy (startingValues, startingValues + numStartingValues, std::back_inserter (cost));
}

如果您使用的是 C++11,那么使用统一的初始化语法可以简化此操作:

State::State()
:
  cost {0, 3, 2, 5, ...}
{
}

首先,cost 成员不是一个数组,它是一个指针(或者当我查看它时,但你已经改变了它。但是,它仍然不是一个数组。它现在是一个编译器错误。 如果你想让它指向一个数组,首先你必须分配它。 但这是一个糟糕的主意。将其更改为向量:

class State {
...
    std::vector<int> cost;
...
};

然后,在你的构造函数中,你可以像这样初始化它(如果你的编译器支持 C++11)

State::State() :
    cost{0, 3,  2,  5,  3,  4} // trimmed for brevity, but you can put as many elements as you want
{}

如果您的编译器不支持 C++11(或不支持此功能),则可以像这样初始化向量:

State::State()
{
    static int temp[] = {0, 3,  2,  5,  3,  4};
    cost.assign(temp, temp + sizeof(temp) / sizeof(*temp));
}