0

I've got a problem with creating an array of strings inside the object. I don't know how to patch it around so I'm asking for help. Here lies the problem:

main.h:

#pragma once

#include <iostream>
#include <conio.h>
#include <string>


class tab2D {

protected:
int width;
int height;
string **sTab;
int **iTab;

public:
tab2D();
tab2D(int x, int y, char c);
~tab2D();
tab2D(tab2D&t);
};

class chess: public tab2D {

public:
chess(int x, int y);
~chess();
chess(chess&c);
void init();
bool ifMove();
void show();
};

class matrix: public tab2D {

public:
matrix(int x, int y);
~matrix();
matrix(matrix&m);
};

The compiler says: syntax error : missing ';' before '*' about the line

string **sTab;

I assume that I can't make the dynamic array of strings and it makes further problems with processing this array.. Can you help me? :)

*UPDATE 1*Thanks, I forgot to add line

using namespace std;

Now it works, but I've got another problem.

#include "main.h"

using namespace std;


////// Konstruktor, konstruktor kopiujący oraz destruktor //////
chess::chess(int x = 8, int y = 8) : tab2D(x, y, 'c') {
        init();
};

chess::chess(chess&c) {
        chess(c.width, c.height);
};

chess::~chess() {
};

////// Metody //////

////// Uzupełnianie kolorów pól oraz rozstawianie figur i pionków //////
void chess::init() {
    /// kolory pól: 0 - biały, 1 - czarny///
    int last = 0;
    for(int i = 0; i < this->height; ++i) {
        for(int j=0; j < this->width; ++j) {
            if(last = 0) {
                this->sTab[i][j] = "1";
                last = 1;
            }
            else if(last = 1) {
                this->sTab[i][j] = "0";
                last = 0;
            }
        }
        if(last = 0)
            last = 1;
        else if(last = 1)
            last = 0;
    };
    /// rozstawienie pionków ///
    for(int i = 0; i < this->width; ++i) {
        sTab[1][i] = sTab[1][i] + "0";
        sTab[6][i] = sTab[6][i] + "a";
    };
};

////// Wyświetlenie szachownicy //////
void chess::show() {
    for(int i = 0; i < (this->height + 1); ++i) {
        for(int j=0; j < (this->width + 1); ++j) {
            if(i == 0 && j == 0)
                cout << "   ";
            else if (i != 0 && j == 0) {
                switch (i) {
                case 1:
                    cout << "A  ";
                    break;
                case 2:
                    cout << "B  ";
                    break;
                case 3:
                    cout << "C  ";
                    break;
                case 4:
                    cout << "D  ";
                    break;
                case 5:
                    cout << "E  ";
                    break;
                case 6:
                    cout << "F  ";
                    break;
                case 7:
                    cout << "G  ";
                    break;
                case 8:
                    cout << "H  ";
                    break;
                default:
                    break;
                }
            }
            else if (i == 0 && j != 0) {
                cout << j << "  ";
            }
            else {
                cout << this->sTab[i-1][j-1] << " ";
            }
        }
        cout << endl;
    };
};

When I run the program, there is a breakpoint in the line

this->sTab[i][j] = "0";

I assume there is something wrong with making the array of strings but I don't understand why exactly there is a breakpoint and can't debug it.
UPDATE 2 Here is the code for tab.cpp:

#include "main.h"

using namespace std;


////// Konstruktor domyślny, konstruktor, konstruktor kopiujący oraz destruktor //////
tab2D::tab2D() {
};

tab2D::tab2D(int x, int y, char c) {

    this->width = x;
    this->height = y;
    if (c == 'm') {
        this->iTab = new int*[this->width];

        for(int i=0;i<this->height;++i)
               this->iTab[i] = new int[this->width];
    }
    else if (c == 'c') {
        this->sTab = new string*[this->width];

        for(int i=0;i<this->height;++i)
               this->sTab[i] = new string[this->width];
    }
    else {
    }
};

tab2D::tab2D(tab2D&t) {
      tab2D(t.width, t.height, 't');
};

tab2D::~tab2D() {
        for(int i=0;i<height;++i)
                delete [] iTab[i];
        delete [] iTab;
        for(int i=0;i<height;++i)
                delete [] sTab[i];
        delete [] sTab;
};
Arkadiusz Galler
  • 153
  • 1
  • 1
  • 8

1 Answers1

1

You need to qualify names from the standard library:

std::string **sTab;
^^^^^

If you're doing what I think you're doing and allocating things with new, then you should consider using std::vector to deal with the quagmire of memory management issues you're about to encounter. If you really want to juggle pointers yourself for some reason, don't forget the Rule of Three.

UPDATE Your new problem might be because the copy constructor is horribly broken:

chess::chess(chess&c) {
    chess(c.width, c.height);
};

This creates and destroys a temporary object, but doesn't initialise the object being constructed, leaving it in an invalid state. You probably don't want to declare a copy-constructor at all, as long as the base class is correctly copyable. If you did need one, it should should be more like:

chess::chess(chess const & c) :    // 'const' so constant objects can be copied
    tab2D(c) // copy the base-class subobject
{
    // do whatever else needs doing
}

Alternatively, the new problem might be due to errors in the tab2D constuctors which you haven't shown us. The best way to track it down is to step through the program with a debugger, checking that everything is correctly initialised before use.

UPDATE Probably, the runtime error is caused by allocating the wrong number of pointers. You want

iTab = new int*[height];  // Not width

and likewise for sTab.

Community
  • 1
  • 1
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • You were very helpfull but I don't quite understand what "vector" is. Could you look up on my edit? My further problem may be connected to it, maybe you will give me some advice :) – Arkadiusz Galler Dec 04 '13 at 15:30
  • @ArkadiuszGaller: [Don't add `using namespace std;`](http://stackoverflow.com/questions/1452721). I'd guess your new problem is that you haven't correctly initialised `sTab` to point to valid memory - perhaps because the copy-constructor of `chess` is horribly broken. `vector` is a class from the standard library that manages a dynamic array, so you don't have to write error-prone code like this. Your introductory book should explain how to use it. – Mike Seymour Dec 04 '13 at 15:37
  • I don't use any copying constructor anyway, they are just declared because our professors require this in code.. They have never taught us to debug programs so I'm very bad at doing this.. I will add the code of tab2d methods, maybe you will look up and help me some more – Arkadiusz Galler Dec 04 '13 at 15:48
  • @ArkadiuszGaller: You've got `width` and `height` mixed up in the initialisation loops. – Mike Seymour Dec 04 '13 at 15:53
  • I found error myself, there was "=" instead of "==" in one of the ifs in tab2D constructor.. Thanks for help anyway :) – Arkadiusz Galler Dec 04 '13 at 15:57
  • Also thanks for noticing error in initialisation loops. Chess class is 8x8 2d tab anyway so it didn't raise any issues, but it would show up in matrix :) – Arkadiusz Galler Dec 04 '13 at 15:59