替换值的多维数组
Multidimensional arrays replacing values
当我打印我的字段时,它可以工作,但是当我更改值时,数组似乎被重置了。我想我把我的字符串veld[10][11]
在错误的地方,但我不是舒尔。还得到了草原作为我班级speelveld.h
的属性谢谢
#include "speelveld.h"
#include "Schip.h"
void spelbord::printVeld(){
//spelbord::zetBoot();
string veld[10][11]= {
{ "A", " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
{ "B", " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
{ "C", " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
{ "D", " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
{ "E", " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
{ "F", " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
{ "G", " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
{ "H", " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
{ "I", " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
{ "J", " ", " ", " ", " ", " ", " ", " ", " ", " ", " " }
};
cout << " 1 2 3 4 5 6 7 8 9 10" << endl;
for (int i = 0; i < 10; i++){
cout << " +-+-+-+-+-+-+-+-+-+-+" << endl;
for (int j = 0; j < 11; j++){
cout << veld[i][j] << "|" << flush;
}
cout << endl;
}
cout << " +-+-+-+-+-+-+-+-+-+-+" << endl;
}
void spelbord::zetBoot(){
string veld[10][11];
cout << "Wat is de eerste coordinaat van je vliegdekschip? (bv: a1) " << flush;
cin >> vliegdekschip1;
y = vliegdekschip1[0];
x = vliegdekschip1[1];
cout << y << endl;
cout << x << endl;
spelbord::checkpos();
}
void spelbord::checkpos(){
if (y == 97){
if (x == 49){
veld[0][1] = "O";
spelbord::printVeld();
}
{
{
对于我的其余答案,我假设您的类spelbord
具有类型为string
的属性veld
。
问题所在
问题是您在spelbord::printVeld
函数中使用了局部变量:
void spelbord::printVeld()
{
/* Initialize a 'new' local variable each time you pass here. */
string veld[10][11] = {
/* Initialization value. */
}
/* Print header. */
for (int i = 0; i < 10; ++i)
{
for (int j = 0; j < 11; ++j)
/* Refers to the variable just initialized. */
cout << veld[i][j] << "|" << flush;
cout << endl;
}
/* Print footer. */
}
void spelbord::checkpos()
{
if (y == 97)
if (x == 49)
{
/* Refers to the attribute 'veld' of the 'spelbord' object */
veld[0][1] = "O";
spelbord::printVeld();
}
}
总而言之,您始终显示一个新初始化的变量。不是您在spelbord::checkpos
中修改的那个.
可能的解决方案
/* Constructor. Used to initialize members. */
spelbord::spelbord()
{
/* I don't use the constructor's prefered way of initialization because your
initialization value is huge. */
veld = { /* Initialization. */ };
}
/* No local variable this time. */
void spelbord::printVeld()
{
/* Print header. */
for (int i = 0; i < 10; ++i)
{
for (int j = 0; j < 11; ++j)
/* Refers to the member of the object. */
cout << veld[i][j] << "|" << flush;
cout << endl;
}
/* Print footer. */
}
void spelbord::checkpos()
{
/* Same as before. */
}
这次成员仅在生成对象时初始化一次,并且您修改并显示此成员。
相关文章:
- 按平均值替换数组中的元素
- 如何将字节数组元素替换为修改的十六进制 ASCII 符号?
- C++ 替换字节数组中项的顺序
- 使用基于数组和范围的 For 循环替换一些基本代码行
- 为什么数组中对象的析构函数在被另一个对象替换时不被调用?
- 替换 2D 数组中的字符
- 如何在给定文本中用字符数组替换符号
- 用现代C++STL数据结构替换旧的C风格数组
- 查找并替换数组的特定元素
- 如果在执行过程中替换二进制文件,"const"数组是否驻留在内存中?
- 我的函数不会用" "替换".",也不会按预期显示数组
- 用分层结构和类替换数组可以吗?
- 如何替换QT QML中的变体数组文本
- 为什么数据中的随机部分替换了曾经存储在我的角色数组中的其他数据
- C++ 在不使用 C 字符串库函数的情况下替换数组中的字符
- 用另一个1D数组替换2D数组的整个行
- 在函数的输入参数中将 char 数组替换为 std::string
- 用另一个字符数组替换一个字符数组
- 在哪里可以找到如何用函数数组替换"switch"运算符的示例?
- 将内置数组替换为std/tr1/boost::数组是否总是安全的?