c++ - 尝试通过指针索引对象数组时出现段错误

标签 c++ arrays pointers sfml

有人能告诉我为什么运行这个会出现段错误吗?我尝试使用指向对象数组的指针,我该如何解决这个问题? 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/

相关文章:

c++ - 在Tensorflow中,使用c++获取图中所有张量的名称

安卓工作室 NDK : could not load library "libgnuSTL_shared.so"

c++ - 将新实例或其自身作为 std::shared_ptr 而不是原始指针返回

C-传递 3d 数组、分配和人口

c++ - 常量指针与指向常量字符串的指针

c - 一个用字符指针创建字符串但在途中出错的实验

c++ - 你如何将 dll 与你的应用程序结合起来?

c++ - 查找背包中带了哪些元素

java - 数组设置不会切断java数组

javascript - 将二维数组从 PHP 转换为 JS