我在对象内创建字符串数组时遇到了问题。我不知道如何修补它,所以我正在寻求帮助。问题就出在这里:
主要.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);
};
编译器说:语法错误:缺少';'关于行
string **sTab;
我假设我不能制作动态字符串数组,并且它会在处理这个数组时产生更多问题。你能帮我吗? :)
*更新 1*谢谢,我忘了添加行
using namespace std;
现在它可以工作了,但是我遇到了另一个问题。
#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;
};
};
当我运行程序时,该行有一个断点
this->sTab[i][j] = "0";
我假设制作字符串数组有问题,但我不明白为什么会有断点并且无法调试它。
更新 2
这是 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;
};
最佳答案
您需要从标准库中限定名称:
std::string **sTab;
^^^^^
如果你正在做我认为你正在做的事情并使用 new
分配东西,那么你应该考虑使用 std::vector
来处理您将要遇到的内存管理问题。如果出于某种原因你真的想自己玩弄指针,请不要忘记 Rule of Three .
更新您的新问题可能是因为复制构造函数严重损坏:
chess::chess(chess&c) {
chess(c.width, c.height);
};
这会创建并销毁一个临时对象,但不会初始化正在构造的对象,使其处于无效状态。您可能根本不想声明复制构造函数,只要基类可以正确复制即可。如果您确实需要一个,它应该更像是:
chess::chess(chess const & c) : // 'const' so constant objects can be copied
tab2D(c) // copy the base-class subobject
{
// do whatever else needs doing
}
或者,新问题可能是由于您未向我们展示的 tab2D
构造函数中的错误造成的。追踪它的最佳方法是使用调试器逐步调试程序,检查所有内容在使用前是否已正确初始化。
更新 运行时错误可能是由于分配了错误数量的指针引起的。你要
iTab = new int*[height]; // Not width
sTab
也是如此。
关于c++ - 对象内的字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20378878/