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();
};
Hawwaru
  • 5
  • 2
  • 5
  • 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 [mcve]. – wally Mar 31 '18 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. – stark Mar 31 '18 at 20:00
  • @Wally Won't work even if they are constant. Can't point at a 2D array like that. – user4581301 Mar 31 '18 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? – user4581301 Mar 31 '18 at 20:09

1 Answers1

1

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

Anton Todua
  • 667
  • 4
  • 15
  • 2
    Best to flip the `vector` of `vector` and array of arrays examples around to get the best one first. – user4581301 Mar 31 '18 at 20:03
  • 1
    Allocating arrays as what you've shown is highly inefficient and error prone. [Here is a better way of doing this](https://stackoverflow.com/questions/21943621/how-to-create-a-contiguous-2d-array-in-c/21944048#21944048) – PaulMcKenzie Mar 31 '18 at 20:48
  • @PaulMcKenzie I agree. I just showed possible solutions for the problem. And my third variant is similar to your proposal. – Anton Todua Mar 31 '18 at 20:51