我有一个重载的=
运算符正确传递 particle
中的一些值类,但不是其他值。我在网上进行了故障排除,但找不到与此问题直接相关的任何内容。我的同事在 C++ 方面相当有能力,但无法提供帮助,并建议我在这里发帖。任何帮助将不胜感激。
width
的所有值, height
, wrap
, particlen
和array
都完美地通过了。但是,xpos
的值, ypos
, xvel
,和yvel
传递的值不正确。对于 xpos
和ypos
,每 11 个元素都正确通过,但所有其他元素都等于 0;我预计这些值都不为零。在 main
,在立即进行的操作中gen1 = gen0;
,gen0 的所有取消引用值都是正确的。我估计他们没有正确通过?如果需要,我会很乐意发布更多代码/信息。再次强调,任何见解都将不胜感激。谢谢。
相关代码:
class particle{
private:
int* xpos;
int* ypos;
int* xvel;
int* yvel;
int* array;
int width;
int height;
int wrap;
int particlen;
public:
void operator=(const particle&);
}
重载=运算符的相关部分:
void particle::operator=(const particle ¤t){
int a,b,i,j;
width = current.width;
height = current.height;
wrap = current.wrap;
particlen = current.particlen;
array = new int[width*height];
xpos = new int[particlen];
ypos = new int[particlen];
xvel = new int[particlen];
yvel = new int[particlen];
for(a=0; a<height; a++){
for(b=0; b< width; b++){
this->array[a*width + b] = current.array[a*width + b];
}
}
for(i = 0; i < particlen; i++){
this->xpos[i] = current.xpos[i];
this->ypos[i] = current.ypos[i];
this->xvel[i] = current.xvel[i];
this->yvel[i] = current.yvel[i];
}
main的相关部分:
int main(){
particle gen0,gen1;
gen1 = gen0;
}
根据Sehe的建议,我编辑了我的代码。但是,我现在使用最简单的命令遇到了内存分配问题。 << 运算符是我的 main 中调用的第一个操作,不允许将我的数组变量设置为等于读入文件中的整数。我确实知道该文件是可读的,因为第一个输出行打印。但是,我随后在 array[i*width + j] = k;
处收到段错误 11 。 GDB 打印输出:程序收到信号 EXC_BAD_ACCESS,无法访问内存。
原因:KERN_INVALID_ADDRESS,地址:0x0000000000000000
粒子::operator<<中的0x000000010001c4f4(this=0x7fff5fbfcb08,file=0x7fff5fbffa90“100100”)在typedef 1 vector.cpp之前:686
第686章
我已经在线查找了 vector 库的正确术语,并且认为我的语法是正确的。有什么想法吗?
更新(美国东部时间 8 月 2 日 9:00)
我仍然遇到 NULL 指针的问题。当使用gdb调试时,我收到消息:
KERN_INVALID_ADDRESS 位于地址: 0x0000000000000000 0x000000010001d807 中,粒子::operator<< (this=0x7fff5fbfcb08, file=0x7fff5fbffa90 "5040") 位于 typedef 1 vector.cpp:688 之前 第688章
我对 NULL 指针的经验很少;关于为什么会发生这种情况以及如何解决它有什么建议吗?下面是相关代码(总代码约900行)
class particle{
private:
std::vector<int> xpos;
std::vector<int> ypos;
std::vector<int> xvel;
std::vector<int> yvel;
std::vector<int> array;
int width;
int height;
int wrap;
int particlen; // number of particles read in
public:
void operator<<(particle);
void Collision(int, int, particle);
void operator>>(char*);
void operator<<(char*);
};
void particle::operator<<(char* file) // Reads initial input file
{
ifstream in_file;
in_file.open( file); // open the file
int i,j,k;
}
in_file >> width >> height >> wrap >> particlen;
for(i=0; i<height; i++){
for(j=0; j< width; j++){
in_file >> k;
array[i*width + j] = k; // This is line 688 which GDB references
}
}
}
void particle::operator<<(particle current){
int i,k,j,l;
for(k = 0; k < height*width; k++)
{
array[k] = 0;
}
k = 0;
for(i=0; i < particlen; i++)
{
cout << "Current x pos is" << current.xpos[i] << endl;
}
for(i=0; i < particlen; i++)
{
if(array[ (current.ypos[i]-1)*width + (current.xpos[i] - 1) ] == 0 )
{
//cout << "Current X position[" << i << "] is" << current.xpos[i] << endl;
xpos[i] = current.xpos[i] + (current.xvel[i])*timeinc;
if(xpos[i] > width) xpos[i] = xpos[i] - width;
ypos[i] = current.ypos[i] + (current.yvel[i])*timeinc;
if(ypos[i] > width) ypos[i] = ypos[i] - height;
//cout << "Next X position[" << i << "] is" << xpos[i] << endl;
}
if(array[ (current.ypos[i]-1)*width + (current.xpos[i] - 1) ] > 1 && current.JC[i] != 1)
{
for(l=i+1; l < particlen; l++)
{
if(current.xpos[l] == current.xpos[i] && current.ypos[l] == current.ypos[i] && current.JC[l] != 1)
{
Collision(i,l,current);
}
}
}
}
for(i=0; i < particlen; i++)
{
array[ (xpos[i]-1)*width + (ypos[i]-1) ] = 1;
}
Display();}
int main()
{
char filename[256];
particle gen0, gen1;
gen0 << filename;
gen1 = gen0;
gen0 << gen1; // Calculates the next state of gen0, based on the values in gen1.
}
我知道这很长,但我认为这是我可以提供的最少信息。我认为问题可能出在gen0 << gen1
运算符,所以我包含了该方法。
最佳答案
就像我在评论中暗示的那样,99% 的人说你违反了“三法则”
值得注意的是,您有赋值运算符,但看不到复制构造函数。这可能意味着如果复制,数组指针最终将“非法”共享。我还假设您在某处有一个析构函数来删除这些数组(在一个实例中),从而使复制到另一个实例的数组无效。
首先通过避免手动内存管理来修复它:
#include <vector>
class particle{
std::vector<int> xpos;
std::vector<int> ypos;
std::vector<int> xvel;
std::vector<int> yvel;
std::vector<int> array;
int width;
int height;
int wrap;
int particlen; // or use `xpos.size()` e.g.
};
int main()
{
particle gen0,gen1;
gen1 = gen0;
}
PS. 每当您需要边界检查时,您可能需要考虑使用 v.at(i)
而不是 v[i]
。这对于调试问题有很大帮助。
关于c++ - 指针值未正确传递给重载的等于运算符 (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18006676/