テクスチャ クラスに画像ファイルのデータを入力するコードを書いています。私が知る限り、コードと実装は機能しますが、プログラムのメイン ループに入るとセグメンテーション エラーが発生します。次の 4 行を削除すると、セグメンテーション違反が解消されます。
Texture* texture = new Texture(); // Dynamically allocate texture object to texture pointer
bool success = loadTexture(texture, "C:/pathToImage/image.png"); // Function that gets image data
cout << success << endl; // Print out success or not
textures.push_back(*texture); // put texture in a vector of textures
編集: texture.h
class Texture {
public:
Texture();
Texture(const Texture&);
~Texture();
Texture& operator=(const Texture&);
public:
void init();
int width, height;
std::vector<unsigned char> pixmap;
GLuint id;
};
texture.cpp:(エラーに関連しないため、init 関数は編集され、呼び出されません。)
Texture::Texture() : width(0), height(0), pixmap(), id(int(-1)){}
Texture::Texture(const Texture& other) : width(other.width), height(other.height), pixmap(other.pixmap), id(other.id){}
Texture::~Texture()
{
width = 0;
height = 0;
delete &pixmap;
id = int(-1);
}
Texture& Texture::operator=(const Texture& other) {
width = other.width;
height = other.height;
pixmap = other.pixmap;
id = other.id;
}
テクスチャ ポインターと関係があると思いますが、これと同じことを行ういくつかの方法を試しましたが、すべて同じセグメンテーション エラーが発生しました。誰かがこれの原因を説明できますか?