设置类级别2D在值中传递的数组变量 - C ++ [英] Setting class level 2D Array variable to passed in value - C++

查看:112
本文介绍了设置类级别2D在值中传递的数组变量 - C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个迷宫解决方案,虽然算法是健全的(至少在我的头上),我一直在运行到一个包装与二维数组。我来自C#和Java,所以语法让我很伤心。



这里是一个SSCCE:

  // Main.cpp 

#includeMazeSolver.h

int main()
{
MazeSolver mazeSolver;

char maze [51] [51] = {}; //不打扰显示填充数组的代码

mazeSolver.setMaze(maze);
}






  // MazeSolver.cpp 

#includeMazeSolver.h

MazeSolver :: MazeSolver(){}

void MazeSolver :: setMaze(char maze [51] [51])$ ​​b $ b {
this-> maze = maze;
}






  // MazeSolver.h 

#ifndef _MAZESOLVER_H
#define _MAZESOLVER_H

class MazeSolver
{
private:
char ** maze; //这很可能是个问题,我在wits end关于
//这里需要什么符号。我不想硬拷贝
//整个数组,我只需要访问它(通过指针)
//我也试过只使用char maze [51] [51]这里,char * maze,etc ...
public:
MazeSolver();
void setMaze(char maze [51] [51]);
}


解决方案

< rant>



这是C ++的奇怪怪癖之一。



C ++ 2D数组不是锯齿状数组。当你声明char maze [51] [51],它实际上分配1个连续数组51 * 51成员长。 sizeof(maze)== 51 * 51 。当你取消引用一个值, maze [a] [b] ,它实际上是 *(maze + 51 * a + b) code>。所有这一切都在底层。



Jagged数组是一个数组数组,一个字符**。在这种情况下,你有一个51指针的数组size ==(51 * sizeof(void *))。在每个位置,指针指向一个完全不同的内存位置,分配给51个成员长。



这是ANNOYING,因为你不能只是转换这两个,铸件。你必须处理古怪的语法,例如 char(* maze)[51] ,以获得指向2D数组的指针。



更令人讨厌的是以下情况:

  int foo(int maze [51] [51] 
{
return sizeof(maze);
}

int maze [51] [51];

int main(int argc,char ** argv)
{
std :: cout< sizeof(maze)<< std :: endl; // return 51 * 51 * sizeof(int);
std :: cout<< foo(maze)<< std :: endl; // return 8,sizeof(void *);
}

所以它隐式通过引用,而不是值,



p> tl; dr;



指向二维数组的指针的正确语法是 char(* maze)[51]; 。你的语法是一个锯齿状的数组(数组数组),在C ++中是不一样的。


I'm attempting to make a maze-solving program, and while the algorithm is sound (at least in my head), I've been running into a roadblock with 2D arrays. I'm coming from C# and Java, so the syntax is giving me grief.

Here's an SSCCE:

//Main.cpp

#include "MazeSolver.h"

int main()
{
     MazeSolver mazeSolver;

     char maze[51][51] = { }; // Not bothering to show code for populating the array

     mazeSolver.setMaze(maze);
}


//MazeSolver.cpp

#include "MazeSolver.h"

MazeSolver::MazeSolver() { }

void MazeSolver::setMaze(char maze[51][51])
{
     this->maze = maze;
}


//MazeSolver.h

#ifndef _MAZESOLVER_H
#define _MAZESOLVER_H

class MazeSolver
{
private:
     char **maze; // This is likely the problem, I'm at wits end in regards to
                  // what notation is needed here.  I do not want to hard-copy
                  // the entire array, I just need to access it (via pointer)
                  // I've also tried just using char maze[51][51] here, char *maze, etc...
public:
      MazeSolver();
      void setMaze(char maze[51][51]);
}

解决方案

<rant>

This is one of the weird quirks of C++.

C++ 2D arrays are NOT jagged arrays. When you declare char maze[51][51], it actually allocates 1 contiguous array 51*51 members long. sizeof(maze) == 51*51. When you dereference a value, maze[a][b], what it actually does is *(maze+51*a+b). All this is under the hood.

A Jagged Array is an array of arrays, a char**. In this case, you have an array of 51 pointers size == (51*sizeof(void*)). In each position, the pointer points to a completely different memory location, allocated to 51 members long.

This is ANNOYING because you can't just convert the two, even by casting. You have to deal with weird syntax, such as char (*maze)[51] to get a pointer to the 2D array.

Whats even more annoying is the following happens:

int foo(int maze[51][51])
{
   return sizeof(maze);
}

int maze[51][51];

int main(int argc, char** argv)
{
   std::cout << sizeof(maze) << std::endl;//return 51*51*sizeof(int);
   std::cout << foo(maze) << std::endl;//return 8, sizeof(void*);
}

So it implicitly passes by reference, not by value, which is opposite all the rest of C++.

</rant>

tl;dr;

The correct syntax for a pointer to a 2D array is char (*maze)[51];. Your syntax is for a jagged array (arrays of arrays) which is NOT the same thing in C++.

这篇关于设置类级别2D在值中传递的数组变量 - C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆