【问题标题】:Making created objects global [C++]使创建的对象全局化 [C++]
【发布时间】:2014-02-15 16:47:37
【问题描述】:

我正在编写一个跳棋游戏,作为面向对象设计的一种实践形式。我得到的错误如下:

首先是代码:

#include <iostream>
#include <stdio.h>

using namespace std;

class Piece {

public:
    int ypos, xpos, index;
    string color, kind;

int getY(void)
{
    return ypos;
}

int getX(void)
{
    return xpos;
}

int adjustY(int change)
{
    ypos = ypos + change;
}

int adjustX(int change)
{
    xpos = xpos + change;
}

};



int form(void)
{
string p[24];
for (int i = 0; i <= 23; ++i)
{
    if (i < 4)
    {
        Piece p[i];
        p[i].color = "white";
        p[i].ypos = 7;
        p[i].xpos = i * 2;
        p[i].index = i;
        p[i].kind = "peon";

    }

    else if (i < 8)
    {
        int l;
        l = i - 4;
        Piece p[i];
        p[i].color = "white";
        p[i].ypos = 6;
        p[i].xpos = 1 + (l * 2);
        p[i].index = i;
        p[i].kind = "peon";
    }

    else if (i < 12)
    {
        int m;
        m = i - 8;
        Piece p[i];
        p[i].color = "white";
        p[i].ypos = 5;
        p[i].xpos = (m * 2);
        p[i].index = i;
        p[i].kind = "peon";
    }

    else if (i < 16)
    {
        int n;
        n = i - 12;
        Piece p[i];
        p[i].color = "black";
        p[i].ypos = 0;
        p[i].xpos = 1 + (n * 2);
        p[i].index = i;
        p[i].kind = "peon";
    }

    else if (i < 20)
    {
        int pp;
        pp = i - 16;
        Piece p[i];
        p[i].color = "black";
        p[i].ypos = 1;
        p[i].xpos = (pp * 2);
        p[i].index = i;
        p[i].kind = "peon";
    }

    else
    {
        int q;
        q = i - 20;
        Piece p[i];
        p[i].color = "black";
        p[i].ypos = 2;
        p[i].xpos = 1 + (q * 2);
        p[i].index = i;
        p[i].kind = "peon";
    }
}
}


char matrix[8][8];


int printt(void)
{
for (int i = 0; i = 7; ++i)
{
    for (int j = 0; j = 7; ++j)
    {
        matrix[i][j] = '_';
    }
}
for (int c = 0; c <= 23;++c)
{
    int a, b;
    a = p[c].ypos;
    b = p[c].xpos;
    switch(p[c].kind)
    {
        case "peon":
            switch(p[c].color)
            {
                case "white":
                    matrix[a][b] = 'o';
                    break;

                case "black":
                    matrix[a][b] = 'x';
                    break;

            }
            break;

        case "damen":
            switch(p[c].color)
            {
                case "white":
                    matrix[a][b] = 'O';
                    break;

                case "black":
                    matrix[a][b] = 'X';
                    break;
            }
            break;
    }

}
cout << "   0|1|2|3|4|5|6|7|  X Position (column)(j)" << endl;
cout << endl;
cout << "0  " << matrix[0][0] << "|" << matrix[0][1] << "|" << matrix[0][2] << "|" << matrix[0][3] << "|" << matrix[0][4] << "|" << matrix[0][5] << "|" << matrix[0][6] << "|" << matrix[0][7] << endl;
cout << "0  " << matrix[1][0] << "|" << matrix[1][1] << "|" << matrix[1][2] << "|" << matrix[1][3] << "|" << matrix[1][4] << "|" << matrix[1][5] << "|" << matrix[1][6] << "|" << matrix[1][7] << endl;
cout << "0  " << matrix[2][0] << "|" << matrix[2][1] << "|" << matrix[2][2] << "|" << matrix[2][3] << "|" << matrix[2][4] << "|" << matrix[2][5] << "|" << matrix[2][6] << "|" << matrix[2][7] << endl;
cout << "0  " << matrix[3][0] << "|" << matrix[3][1] << "|" << matrix[3][2] << "|" << matrix[3][3] << "|" << matrix[3][4] << "|" << matrix[3][5] << "|" << matrix[3][6] << "|" << matrix[3][7] << endl;
cout << "0  " << matrix[4][0] << "|" << matrix[4][1] << "|" << matrix[4][2] << "|" << matrix[4][3] << "|" << matrix[4][4] << "|" << matrix[4][5] << "|" << matrix[4][6] << "|" << matrix[4][7] << endl;
cout << "0  " << matrix[5][0] << "|" << matrix[5][1] << "|" << matrix[5][2] << "|" << matrix[5][3] << "|" << matrix[5][4] << "|" << matrix[5][5] << "|" << matrix[5][6] << "|" << matrix[5][7] << endl;
cout << "0  " << matrix[6][0] << "|" << matrix[6][1] << "|" << matrix[6][2] << "|" << matrix[6][3] << "|" << matrix[6][4] << "|" << matrix[6][5] << "|" << matrix[6][6] << "|" << matrix[6][7] << endl;
cout << "0  " << matrix[7][0] << "|" << matrix[7][1] << "|" << matrix[7][2] << "|" << matrix[7][3] << "|" << matrix[7][4] << "|" << matrix[7][5] << "|" << matrix[7][6] << "|" << matrix[7][7] << "   Y Position (line)(i)" << endl;
cout << endl;
}


int main()
{
form();
printt();
cout << "End of Programm" << endl;
system ("pause");
return 0;
}

然后报错:

在函数'int printt()'中:

第 130 行第 7 列 [错误] 'p' 未在此范围内声明

我认为问题在于对象是在外部函数中创建的。但是,使用 extern 会产生比我已经遇到的更多的问题,并且不可能在函数外部创建它们,因为“for (int i=0;i

我的问题是:如何将这些对象(Piece p[0]、Piece p[1] 等)调用到 printt() 函数?

非常感谢,如果问题很愚蠢,对不起,我对编程很陌生。

【问题讨论】:

  • 你不想拥有全局变量!它让一切变得更糟......

标签: c++ arrays object global-object


【解决方案1】:

你应该这样定义:

Piece p[24];

在全局范围内,例如在 f: 的定义之前

Piece p[24];

int form(void)
{
    for (int i = 0; i <= 23; ++i)

您在printt 函数中引用这些对象的方式是正确的。

另外,你应该删除

Piece p[i]; 

来自int from() 实现的声明。

此外,您可能希望为 Piece 类实现一个默认构造函数,以确保实例的 int 字段在构造时得到合理初始化。

【讨论】:

  • @Martin_J 这给了我第 131/132/135 行和许多其他错误:[Error] 'std::string' has no member named 'ypos' 所以我不能使用p[1].ypos,因为它从列表中获取字符串,而不是对象。
  • 那是因为你在你的函数中声明了一个string p[24],但你没有使用它并且隐藏了全局p
  • 谢谢,现在我没有错误了!我现在遇到开关问题,但这是另一回事。显然不能使用字符串作为开关值。谢谢你们的帮助。
  • @martin-j 我不确定如何创建构造函数。我该怎么做?
  • 在你的类声明中:Piece() : ypos(-1), xpos(-1), index(-1), color(), kind() {},但我强烈建议你查一下这是什么意思。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多