我正在编写代码来用图像文件中的数据填充纹理类。据我所知,代码和实现是有效的,但是在进入我的程序的主循环时会导致分段错误。这 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;
}
我假设它与纹理指针有关,但是我尝试了几种方法来做同样的事情,它们都导致了相同的分段错误。有人可以解释是什么原因造成的吗?