Segmentation Fault when trying to index an array of arrays of objects via a pointer
Can someone please tell me why I get a segmentation fault running this? I try and a pointer to an array of an array of objects, how I can fix this problem? The declaration of the sf::Vector2 class can be found here: http://www.sfml-dev.org/documentation/1.6/classsf_1_1Vector2.php
Many thanks.
#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),
开发者_如何学Python 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();
}
EDIT: g++ compiler
You never allocate or instantiate the types
array you are referencing. types
is a pointer you can't assign concrete values to a nullptr
which is how you left it at the moment.
Just declare it as an array instead of a pointer
sf::Vector2<int> types[2][4];
You may want to consider a simpler more effective design perhaps by having a Vector2
object, a Matrix
object, and then the Tet
object which has a collection of matrices using STL containers and algorithms preferably.
maybe allocate types first and initialize with { &I, &J }
精彩评论