有人能告诉我为什么运行这个会出现段错误吗?我尝试使用指向对象数组的指针,我该如何解决这个问题? sf::Vector2 类的声明可以在这里找到:http://www.sfml-dev.org/documentation/1.6/classsf_1_1Vector2.php
非常感谢。
#include <SFML/System/Vector2.hpp>
#include <iostream>
class Tet
{
public:
Tet();
private:
static sf::Vector2 <int> I[4];
static sf::Vector2 <int> J[4];
static sf::Vector2 <int> *types[2];
};
sf::Vector2 <int> Tet::I[4] = {sf::Vector2 <int>(0,1),
sf::Vector2 <int>(1,1),
sf::Vector2 <int>(2,1),
sf::Vector2 <int>(3,1)};
sf::Vector2 <int> Tet::J[4] = {sf::Vector2 <int>(1,1),
sf::Vector2 <int>(2,1),
sf::Vector2 <int>(3,1),
sf::Vector2 <int>(3,2)};
sf::Vector2 <int>* Tet::types[2] = { I,J };
Tet::Tet()
{
//trying to print out x member of first vector of I
std::cout << (*(*(types))).x << std::endl;
}
main()
{
Tet t = Tet();
}
编辑:g++ 编译器
最佳答案
您永远不会分配或实例化 types
您正在引用的数组。 types
是一个指针,您不能将具体值分配给 nullptr
这就是你现在离开它的方式。
只需将其声明为数组而不是指针
sf::Vector2<int> types[2][4];
您可能想要考虑一个更简单、更有效的设计,也许可以使用 Vector2
对象,一个 Matrix
对象,然后是 Tet
具有矩阵集合的对象,最好使用 STL 容器和算法。
关于c++ - 尝试通过指针索引对象数组时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7560964/