【发布时间】:2015-07-04 19:01:41
【问题描述】:
我有一个类从另一个类创建对象数组(数组类是朋友)
class vector_XXL
{
int i=0,n=0;
XXL_nr *XXLvector;
public:
vector_XXL(int y)
{
n = y;
XXLvector = new XXL_nr[y];}
n 是数组的大小。 XXL_nr 是创建表示为链表的大数字的类(每个数字在链表上都有一个位置)
我想要来自相同位置的 2 个数组对象的每 2 个数字的乘积。因此,例如 v[i]*d[i] ,对于 i = 0,n;
这是应该执行此操作但无法正常工作的函数:
void produs_scalar(vector_XXL A)
{
XXL_nr temp(2),temp1(2),temp2(2),result(2);
vector_XXL vector_result(n);
temp1 = this->XXLvector[0];
temp2 = A.XXLvector[0];
temp1.addition(temp2);
for(i=0;i<n;i++)
{ temp = this->XXLvector[i].product(A.XXLvector[i]);
vector_result.XXLvector[i] = temp;
}
for(i=0;i<n;i++)
result= temp.product(vector_result.XXLvector[i]);
result.print();
}
我使用 temp1 和 temp2 来测试是否可以对 2 个 XXL_nr 变量使用乘积方法(在 XXL_nr 类中定义)。但结果是一样的。问题是,在第一个 for 循环中,它执行产品,它返回 `this->XXLvector[i].product(A.XXLvector[i]); 的正确答案;但它并没有移动到第 i 个位置,整个程序只是在 product 方法返回后挂在那里。 现在,我相信数组是问题所在,因为我尝试在不使用数组(仅 XXL_nr 类型)的情况下做产品,并且该方法不再挂起。示例:
void produs_scalar()//vector_XXL &A)
{
XXL_nr B(2), C(2), D;
cout<<"insert number B";
cin>> B;
cout<<"number C";
cin>>C;
D=B.product(C);
cout<<D;
在这里,一切正常。产品方法返回,代码前进到 cout<<D; 。
我读到一个主题,当程序像这样挂起时,这是因为指向随机内存的东西而程序只是挂起,但我不知道如何调试它或如何验证是否是这种情况。
【问题讨论】:
标签: c++ arrays object linked-list