本文介绍了CArray,我在做什么错?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

头文件中的声明:

Declaration in header file:

CArray<int[1][1],int[1][1]> ptArray;



源文件



Source file

void cDlgGrid::OnClick(NMHDR *pNotifyStruct, LRESULT* pResult)
{
        LPNMITEMACTIVATE pNMIA = reinterpret_cast<lpnmitemactivate>(pNotifyStruct);
        int row = pNMIA->iItem;
	int col = pNMIA->iSubItem;

        ptArray.SetSize(1,1);
	int SelectedCell[1][1];
        SelectedCell[0][0] = row;
	SelectedCell[0][1] = col;
        ptArray.Add(SelectedCell);  --> I am getting an error here

</lpnmitemactivate>



错误:



error:

C2664: ''CArray<type,arg_type>::Add'' : cannot convert parameter 1 from
''int (*)[1][1]'' to ''int [][1]''
        with
        [
            TYPE=int [1][1],
            ARG_TYPE=int [1][1]
        ]
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast



我该怎么办.



What should i do.

推荐答案

ptArray.Add(*SelectedCell);



或将CArray声明更改为:



or change the CArray declaration to:

CArray<int[1][1],&int[1][1]> ptArray;



但这是您最少的问题.

您已将SelectedCell声明为1行1列的二维数组.这意味着它仅包含1个整数.

当您分配给SelectedCell [0] [1]时,您将分配给数组范围之外的位置.其结果是不确定的.

在我看来,您实际上想要执行以下操作:



But that''s the least of your problems.

You''ve declared SelectedCell as a 2 dimensional array of 1 row and 1 column. Which means it contains only 1 integer.

When you assign to SelectedCell[0][1] you are assigning to a location outside the bounds of the array. The results of that are undefined.

It seems to me that you actually want to do something like this:

struct SPoint { int row; int col; };

CArray<SPoint,&SPoint> ptArray;


//...

SPoint SelectedCell;
SelectedCell.row = row;
SelectedCell.col = col;

ptArray.Add(SelectedCell);


int SelectedCell[1][1]; // this is exact one int
    SelectedCell[0][0] = row;
SelectedCell[0][1] = col; // this will possible throw a runtime error



问候.



Regards.


这篇关于CArray,我在做什么错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 12:04