我无法将数组插入堆栈。我认为这非常简单,但我已经花了太多时间试图弄明白这一点。
我希望能够像压入整数或 float 一样压入数组,但这并没有发生。
推送命令给了我这个问题。这是我的代码:
#include <iostream>
#include <stack>
struct Matrix4x4
{
float data[16];
};
int main(int argc, char **argv)
{
// My original code
typedef std::stack<float[16]> myStack;
myStack modelViewStack;
myStack projectionStack;
float testMat[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
modelViewStack.push(testMat); // THIS LINE GIVES ME ERRORS
////Stack initialization - This is thokra's solution
////typedef std::stack<std::vector<float[16]>> myStack;
//typedef std::stack<Matrix4x4> myStack;
//myStack modelViewStack;
//myStack projectionStack;
//
//Matrix4x4 m = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
////std::vector<float> testMat2(testMat, testMat + sizeof(testMat) / sizeof(float));
//modelViewStack.push(m);
//for(int i = 0; i<16 ; i++)
//{
// std::cout << "m data: " << m.data[i] << std::endl;
//}
//system("pause");
return 0;
感谢您的帮助!
这是错误。我无法破译它们。也许对如何阅读这些内容的解释也会有所帮助。
1>------ Build started: Project: opengl4_4, Configuration: Release Win32 ------
1> main.cpp
1>C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\xmemory0(606): error C2075: 'Target of operator new()' : array initialization needs curly braces
1> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\xmemory0(605) : while compiling class template member function 'void std::allocator<_Ty>::construct(_Ty (*),const _Ty (&))'
1> with
1> [
1> _Ty=float [16]
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\xmemory0(751) : see reference to function template instantiation 'void std::allocator<_Ty>::construct(_Ty (*),const _Ty (&))' being compiled
1> with
1> [
1> _Ty=float [16]
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\type_traits(743) : see reference to class template instantiation 'std::allocator<_Ty>' being compiled
1> with
1> [
1> _Ty=float [16]
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\deque(925) : see reference to class template instantiation 'std::is_empty<_Ty>' being compiled
1> with
1> [
1> _Ty=std::allocator<float [16]>
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\stack(21) : see reference to class template instantiation 'std::deque<_Ty>' being compiled
1> with
1> [
1> _Ty=float [16]
1> ]
1> main.cpp(14) : see reference to class template instantiation 'std::stack<_Ty>' being compiled
1> with
1> [
1> _Ty=float [16]
1> ]
1>C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\xmemory0(606): fatal error C1903: unable to recover from previous error(s); stopping compilation
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
最佳答案
只需将包含矩阵元素的数据存储封装在合适的类型中:
#include <stack>
struct Matrix4x4
{
float data[16];
};
int main()
{
typedef std::stack<Matrix4x4> myStack;
myStack modelViewStack;
Matrix4x4 m = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
modelViewStack.push(m);
return 0;
}
更重要的是:std::stack::push
将在内部调用 push_back
在 std::deque
上当您不更改默认值时,它用作容器。本质上,当试图在 deque
的末尾构造一个新元素时容器尝试将新元素放置在当前标记容器末尾的地址处 - new
.例如,g++ 实现如下:
template<typename _Up, typename... _Args>
void
construct(_Up* __p, _Args&&... __args)
{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
这实际上归结为:
::new((void *)__p) float[16](_args); // where __p is a pointer to float[16] and _args is testMat
尝试复制或移动初始化 C 数组是不合法的。即使由于某种原因构建成功,容器也会尝试在 float[16]
类型的元素上调用析构函数。当超出范围时。很容易看出,一个析构函数 ~T[n]
不存在。
在 C++11 中,你可以 push
一个std::array<float,16>
而不是定义一个额外的类型。
关于C++ 标准库堆栈使用。无法推送 float 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21339156/