C++ 传递std::vector<;sf::矩形形状>;SFML中的数组

C++ 传递std::vector<;sf::矩形形状>;SFML中的数组,c++,sfml,C++,Sfml,我现在正在处理一个SFML项目,我必须将一个矩形数组传递给一个void函数 std::vector<sf::RectangleShape> shape(16); void setProperties(shape); std::矢量形状(16); 孔隙性质(形状); 该函数尚未生成,但Visual Studio给了我一个错误 void setProperties(std::vector<sf::RectangleShape> shapes(16)) { } v

我现在正在处理一个SFML项目,我必须将一个矩形数组传递给一个void函数

std::vector<sf::RectangleShape> shape(16);  
void setProperties(shape);  
std::矢量形状(16);
孔隙性质(形状);
该函数尚未生成,但Visual Studio给了我一个错误

void setProperties(std::vector<sf::RectangleShape> shapes(16))
{
}
void setProperties(标准::向量形状(16))
{
}
但是这个代码似乎不起作用。如果你能帮助我,那就太好了

void setProperties(std::vector<sf::RectangleShape> shapes(16))
但这会将std::vector的副本传递给函数,这可能不是您想要的(因为它很昂贵)。如果不需要修改向量,最好传递引用或常量引用,例如:

void setProperties(const std::vector<sf::RectangleShape>& shapes)
void setProperties(const std::vector&shapes)
(16)调用std::vector的构造函数。不能在函数定义中使用函数调用,因此需要更改:

void setProperties(std::vector<sf::RectangleShape> shapes(16)){ ... }
void setProperties(std::vector shapes(16)){…}

void setProperties(std::vector shapes){…}

声明变量
shape
时,将值16传递给类
std::vector
的构造函数。构造函数是在对象构造期间操作的函数。 但是,当您声明函数
setProperties
时,您是在确定传递给函数的参数,而不是初始化这些参数。声明参数的构造函数时,不能向其提供参数。尝试这样声明和定义函数:

void setProperties(std::vector<sf::RectangleShape> shapes)
{
    // Set properties here.
}
void集合属性(标准::向量形状)
{
//在这里设置属性。
}

还值得注意的是,由于您是通过值传递的,因此向量的副本将传递到函数中,而不是原始向量。。。如果你想让函数影响
形状的值,你应该通过引用来传递它。

我现在修复了它。我不知道这是否是一个好的解决方案,但它对我有效

我将
void
更改为
std::vector

所以现在是这样的:

std::vector<sf::RectangleShape> shape(16);  
shape = setProperties(shape);  

std::vector<sf::RectangleShape> setProperties(std::vector<sf::RectangleShape> shapes)
{ ... }
std::矢量形状(16);
形状=设置属性(形状);
std::vector集合属性(std::vector形状)
{ ... }
函数声明中的
(16)
有什么关系?你认为这是什么意思?它现在在“void setProperties(shape)”中说“不允许不完整的类型”;
void setProperties(std::vector<sf::RectangleShape> shapes)
{
    // Set properties here.
}
std::vector<sf::RectangleShape> shape(16);  
shape = setProperties(shape);  

std::vector<sf::RectangleShape> setProperties(std::vector<sf::RectangleShape> shapes)
{ ... }