0

I'm writing assignment (creating virtual world) and I've encountered one thing I can't go over. I have class Organism which is an abstract for all the future inheriting ones (animals etc.) and class World which represents the world of these objects. In the latter I can't create an array of Organisms though - to store position of every existing organism.

It throws: "syntax error : missing 'token1' before 'token2'

I was thinking that maybe it has something to do with that it refers to each other (organism has reference to certain world and certain world wants to create array of organisms) but default constructor is solving that thing imo.

Could you tell what have I overseen? Thanks in advance.

World.h

#pragma once
class World
{
private:
    int sizeX;
    int sizeY;
    Organism * worldMesh;
public:
    World(int sizeX, int sizeY);
    void nextTurn();
    void drawWorld();
    int getWorldX();
    int getWorldY();
~World();
};

World.cpp

#include "stdafx.h"
#include "World.h"
#include <iostream>

using namespace std;

World::World(int sizeX, int sizeY)
{
    this->sizeX = sizeX;
    this->sizeY = sizeY;
    worldMesh = new Organism[sizeX][sizeY];
    for (int i = 0; i < sizeX; i++) {
        for (int j = 0; j < sizeY; j++) {
            worldMesh[i][j] = NULL;
        }
    }
} ....

Organism.h

#pragma once
#include "World.h"
#include "Position.h"


class Organism
{
protected:
    int strength;
    int initiative;
    Position * position;
    static World * world;
public:
    Organism();
    Organism(World * world);
    int randValue();
    virtual void spawn();
    virtual void action();
    virtual void collision();
    void kill();
    virtual ~Organism();
};
4
  • I don't see the error in this code. Note that new Organism[sizeX][sizeY] won't work without a non-standard extension if the sizeY isn't a constant. Please post a minimal reproducible example. Commented Mar 31, 2018 at 19:58
  • worldMesh is a pointer. It is not a 2D array. Use std::array or std::vector if you want it to know its shape. Commented Mar 31, 2018 at 20:00
  • @Wally Won't work even if they are constant. Can't point at a 2D array like that. Commented Mar 31, 2018 at 20:00
  • Unrelated. Lot of pointers in there. Remember the big downside of pointers: Someone has to clean up. You sure you want pointers? Commented Mar 31, 2018 at 20:09

1 Answer 1

2

There is no such new Type[size1][size2] construction in C++, but you can do something like this:

int** mesh = new int*[size1];
for( int i = 0; i < size1; i++ ) {
    mesh[i] = new int[size2];
    for( int j = 0; j < size2; j++ ) {
        mesh[i][j] = 0;
    }
}

or just use std::vector

std::vector< std::vector<Type> > mesh;

or use single std::vector with size size1 * size2 and calculate index fromi and j: index = i * size1 + j

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

3 Comments

Best to flip the vector of vector and array of arrays examples around to get the best one first.
Allocating arrays as what you've shown is highly inefficient and error prone. Here is a better way of doing this
@PaulMcKenzie I agree. I just showed possible solutions for the problem. And my third variant is similar to your proposal.

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.