1

I am migrating a scientific code from Java to C++. Please tell me:

a) What's wrong with the two functions?
b) How can I solve the problem? I can use the int** like a two dimensional array but not the Agent**.

I receive this error: "No operator = matches this operand".

In normal C we could assign NULL to pointers. We could also use a type** like a two dimensional array (i.e. a[i][j]) (two dimensional space for objectSpace and agentSpace is allocated somewhere else).

    int** objectSpace;
    Agent** agentSpace;

    void Space::removeAgentAt(Point p)
    {
        agentSpace[p.x][p.y] = NULL;
    }

    void Space::putAgentTo(Agent agent, Point p)
    {
        agentSpace[p.x][p.y] = agent;
    }

5 Answers 5

3

agentSpace is a pointer, and agentSpace[p.x] is a pointer, but agentSpace[p.x][p.y] is not a pointer.

C++ is not the same as Java; objects are (usually) referred to by value, not by reference.

Sign up to request clarification or add additional context in comments.

4 Comments

Oli, I need to have an array of Agents and modify the array to show where in the space are the agents. Could you possibly give a bit advise or kindly a solution?
@wmac do you know the dimensions of the array at compile time, or only at runtime? You are likely better off using a standard library container.
Unfortunately dimensions are unknown but the space is too large. It may contain 1000*1000 cells. I thought that's too much for containers.
@wmac if the dimensions are too large for a container of containers (vector of vectors seems to fit your use-case best) then they are too large for a dynamically allocated array.
1

Look at it like this, for Agent** agentSpace, the first * gives you access to the first dimension of the array, the second * gives you access to the second dimension.

Agent** is a pointer to pointers - or in your case an array of pointers. When you attempted to do agentSpace[p.x][p.y] = NULL, you were trying to assign NULL in to what the compiler thinks is a full object of type Agent You need another level of indirection:

Agent*** agentSpace = 
    {
         { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 },
         { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 },
         { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }
    };

Comments

1

In C++, NULL is just an alias for 0. Unless you have coded conversion from integers to Agent objects, you can't assign NULL to an Agent.

One solution is to make a dummy Agent object, like Agent AgentNone; and use that instead of NULL. You might have to implement comparison operators if you want to check if an Agent object is equal to AgentNone.

Comments

0

Guess you need to define an assignment operator for your Agent class?

Comments

0

To make the code easier to match against the original, provide the array dimensions:

int   objectSpace[100][200];   // but use the actual dimensions
Agent agentSpace[250][300];    // todo:  fix dimensions

Even better would be to use defined constants:

int   objectSpace[OS_X_MAX][OS_Y_MAX];
Agent agentSpace[AS_X_MAX][AS_Y_MAX];

If this is in a header file seen by all modules, then the code you have will work correctly. If there are any places where one of these arrays is passed as a parameter, then the function prototype and implementation likely will have to be rewritten.

3 Comments

The size of arrays is not known. I am using dynamic array allocation templates in this page: codeproject.com/Articles/21909/…
@wmac then I would suggest the vector of vector approach
I have already used that method to create the arrays. It works with integers (like the example on that page). But objects of the type Agent won't work with a two dimensional dynamic array of Agent.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.