0

I'm programming a checkers game as a form of practice for object-oriented design. The error I'm getting is the following:

First the code:

#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;
}

Then the error:

In function 'int printt()':

Line 130 Col 7 [Error] 'p' was not declared in this scope

I assume the problem is that the objects were created in an outside function. However, using extern creates more problems than I already have, and creating them outside a function is not possible, for the "for (int i=0;i<=23;++i)" statement needs to happen inside a function.

My question is: How do I call these objects (Piece p[0], Piece p[1], and so on) to the printt() function ?

Thank you very much, sorry if the question is dumb, I am pretty new to programming.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
user3258512
  • 323
  • 1
  • 3
  • 10

1 Answers1

0

You should put this definition:

Piece p[24];

at the global scope, i.e. for instance just before the definition of f:

Piece p[24];

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

The way you are referencing these objects in the printt function is correct.

Also, you should remove the

Piece p[i]; 

statements from the int from() implementation.

Also, you may want to implement a default constructor for your Piece class, to ensure that instances' int fields will be reasonably initialized on construction.

Martin J.
  • 5,028
  • 4
  • 24
  • 41
  • @Martin_J This gives me the error back in line 131/132/135 and many others: [Error] 'std::string' has no member named 'ypos' So then i can't use p[1].ypos, because it gets the string from the list, not the object. – user3258512 Feb 15 '14 at 16:54
  • That's because you declared a `string p[24]` in your function which you are not using and which hides the global `p` – Nicola Musatti Feb 15 '14 at 16:58
  • Thanks, now I get no error! I'm having problems with switch now but that's another story. Apparently can't use strings as switch values. Thanks for the help guys. – user3258512 Feb 15 '14 at 17:00
  • @martin-j I'm not sure how I can create a constructor. How should I do that? – user3258512 Feb 15 '14 at 17:14
  • Inside of your class declaration: `Piece() : ypos(-1), xpos(-1), index(-1), color(), kind() {}`, but I highly suggest you lookup what that means. – Martin J. Feb 15 '14 at 17:19
  • @MartinJ Is that so i can make something like: Piece p[1](7,7,10,"white","peon"); to create a piece? – user3258512 Feb 15 '14 at 17:23
  • In the case I suggested, it's only so your default-initialized Piece instances have non-random values. Non-default constructors are used for the kind of case you describe. – Martin J. Feb 15 '14 at 17:28
  • @MartinJ After further development of program, I can confirm this didn't work. The Piece p[24], while giving me no errors, doesn't define any object, doesn't create any. What can I do? – user3258512 Feb 16 '14 at 03:18