Click here to Skip to main content
15,905,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted

There are a number of things wrong here.

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

ptArray.Add(*SelectedCell);


or change the CArray declaration to:

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


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);
 
Share this answer
 
v2
Comments
Olivier Levrey 1-Aug-11 12:38pm    
I agree. The struct solution seems clearer and more appropriate. Have a 5.
amarasat 1-Aug-11 14:09pm    
Thanks a lot for the reply and solution. I gave a 5 for clearing my problem and proposing a new solution, and understanding my situation.


Thanks again, its working well now.
TRK3 1-Aug-11 14:16pm    
Glad it worked for you.

(PS. Fixed the typos in my solution and added the appropriate html ampersand code so that my declaration doesn't disappear.)
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:
C#
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.
 
Share this answer
 
Comments
amarasat 1-Aug-11 14:10pm    
Struct helped me in solving my error.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900