CArray,我在做什么错? [英] CArray, what am i doing wrong?

查看:73
本文介绍了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.

推荐答案

这里有很多错误.

要解决编译器错误,您需要执行以下操作:

There are a number of things wrong here.

To fix the compiler error you''d need to do this:

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);


问题是:如果存在多个可能的强制转换,则始终会出现此编译器错误.您必须强制转换参数.因此,更好的解决方案是仅使用一种可能的类型转换使用结构类型,编译器可以隐式转换类型.
顺便说一句:
The problem is: if there is more than one possible cast you will get always this compiler error. You have to cast explicit the parameter. Therfore its the better solution to use struct types with only one possible cast, the compiler can implicit cast the type.
btw:
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,我在做什么错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆