Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
结构中的数组,指针[C+;+;初学者] 来自java,PHP的后台,我尝试进入C++。我想在结构中存储一个数组。我的问题是在初始化结构后指定数组的大小_C++_Arrays_Pointers_Struct - Fatal编程技术网

结构中的数组,指针[C+;+;初学者] 来自java,PHP的后台,我尝试进入C++。我想在结构中存储一个数组。我的问题是在初始化结构后指定数组的大小

结构中的数组,指针[C+;+;初学者] 来自java,PHP的后台,我尝试进入C++。我想在结构中存储一个数组。我的问题是在初始化结构后指定数组的大小,c++,arrays,pointers,struct,C++,Arrays,Pointers,Struct,以下是我的结构代码: struct SpriteAnimation { // ... int parts; // total number of animation-parts unsigned int textures[]; // array to store all animation-parts // ... }; 以下是主要功能: SpriteAnimation bg_anim; bg_anim.parts = 3

以下是我的结构代码:

struct SpriteAnimation {
    // ...
    int parts;                  // total number of animation-parts
    unsigned int textures[];    // array to store all animation-parts
    // ...
};
以下是主要功能:

SpriteAnimation bg_anim;
bg_anim.parts = 3; 
unsigned int *myarray = new unsigned int[bg_anim.parts];
bg_anim.textures = myarray;
我需要更改什么来修复此问题

struct SpriteAnimation {
    // ...
    int parts;                  // total number of animation-parts
    unsigned int * textures;    // array to store all animation-parts
    // ...
};

您可以使用<代码>类型名称[]/Cord>语法,仅当您声明成员为内联。

在现代C++中,您将使用一个动态容器来实现内部“数组”:

struct SpriteAnimation{
std::vector textures;//用于存储所有动画部分的数组
size_t num_parts()常量{return textures.size();}
};
这比手动分配的存储更安全、更模块化。用法:

SpriteAnimation x;
x.textures.push_back(12);  // add an element
x.textures.push_back(18);  // add another element

SpriteAnimation y = x;     // make a copy

std::cout << "We have " << x.num_textures() << " textures." std::endl; // report
SpriteAnimation x;
x、 纹理。向后推(12);//添加一个元素
x、 纹理。向后推(18);//添加另一个元素
SpriteAnimation y=x;//复印

std::cout编译时必须知道结构的大小。

我通过以下代码解决了这个问题。它可能有设计问题,所以请查找以下代码

#include <iostream>
using namespace std;
struct lol {
  // ...
  int parts;                  // total number of animation-parts
  unsigned int *texture;   // array to store all animation-parts
  // ...
};

int main() {
  // your code goes here
  lol bg_anim;
  bg_anim.parts = 3; 
  unsigned int *myarray = new unsigned int[bg_anim.parts];
  bg_anim.texture = myarray;
  return 0;
 }
#包括
使用名称空间std;
结构lol{
// ...
int parts;//动画部分的总数
unsigned int*texture;//用于存储所有动画部分的数组
// ...
};
int main(){
//你的密码在这里
lol bg_动画;
bg_anim.parts=3;
unsigned int*myarray=新的unsigned int[bg_anim.parts];
bg_anim.texture=myarray;
返回0;
}
请原谅我使用
lol
而不是您指定的名称。请告诉我任何问题。如果我的代码中存在其他问题,请帮助我。
谢谢!!:)

唐纳,你能解释一下你的反对意见吗?我理解这不是做事情的“C数组方式”,但是问题需要C++解决方案,C++的习惯和道德的方法是使用(异常安全和资源管理和单责任)容器作为构建块。(或
vector
-请您解释一下区别好吗)?
push_back()
通过每次调用时创建一个新的来调整它的大小?与John的解决方案相比,这将是非常低效的,或者我误解了什么吗?@Ben:解释
向量的完整工作原理超出了这一注释的范围,但是--是的,
push_back
确实附加了一个新元素,但这是摊销O(1)事实上,大多数C++标准库只能被描述为这样。它是正确的,然后使用它“代码> BGYIMAN。纹理=新的未签名int [BGyAymi.Posits ]。当你复制你的结构时会发生什么?当有异常发生时会发生什么?@本是的,它工作正常,实际上它以前是不正确的,因为纹理是一个动态分配的数组,应该被显示为指针。如果你想使用C++,因为你使用C风格的语法,你应该查找类。末尾的数组他想使用指针,类型unsigned int*是一个静态大小,正如
struct
只不过是一个所有内容都是公共的类一样。更好的解决方案是使用类。
#include <iostream>
using namespace std;
struct lol {
  // ...
  int parts;                  // total number of animation-parts
  unsigned int *texture;   // array to store all animation-parts
  // ...
};

int main() {
  // your code goes here
  lol bg_anim;
  bg_anim.parts = 3; 
  unsigned int *myarray = new unsigned int[bg_anim.parts];
  bg_anim.texture = myarray;
  return 0;
 }