2 Dimensional Vectors

Hi, I am having some compilation issues when using a two dimensional vector. Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "data.h"
#include <vector>
#include <iostream>

using namespace std;

int main() {
    vector<vector<cell> > grid;
    grid.resize(23);
    for(int i=0; i<23; i++)
        grid[i].resize(40);
    for(int i=0; i<23; i++)
        for(int j=0; j<40; j++)
            grid[i][j].setCoord(i, j);
    return 0;
}


If you're wondering what cell is, here is where I defined it in a separate header file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
enum cell_type{player, beast, movable, immovable, empty};

struct point{
private:
    int X, Y;
    
public:
    point();
    point(int x, int y);
    
    int x();
    void x(int);
    int y();
    void y(int);
};

class cell{
protected:
    point coord;
    cell_type type;
    
public:
    cell();
    cell(point&);
    virtual ~cell();
    
    point getCoord();
    void setCoord(point&);
    void setCoord(int&, int&);
    virtual cell_type getType()=0;
    virtual void print(std::ostream &)=0;
};


Here are my errors:
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_vector.h: In member function `void std::vector<_Tp, _Alloc>::resize(size_t) [with _Tp = cell, _Alloc = std::allocator<cell>]':
h3main.cpp:11:   instantiated from here
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_vector.h:412: error: cannot allocate an object of type `cell'
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_vector.h:412: error:   because the following virtual functions are abstract:
data.h:36: error:  virtual void cell::print(std::ostream&)
data.h:35: error:  virtual cell_type cell::getType()
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_construct.h: In function `void std::_Construct(_T1*, const _T2&) [with _T1 = cell, _T2 = cell]':
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_uninitialized.h:86:   instantiated from `_ForwardIterator std::__uninitialized_copy_aux(_InputIterator, _InputIterator, _ForwardIterator, __false_type) [with _InputIterator = __gnu_cxx::__normal_iterator<const cell*, std::vector<cell, std::allocator<cell> > >, _ForwardIterator = cell*]'
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_uninitialized.h:112:   instantiated from `_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const cell*, std::vector<cell, std::allocator<cell> > >, _ForwardIterator = cell*]'
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_vector.h:221:   instantiated from `std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = cell, _Alloc = std::allocator<cell>]'
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_vector.h:412:   instantiated from `void std::vector<_Tp, _Alloc>::resize(size_t) [with _Tp = std::vector<cell, std::allocator<cell> >, _Alloc = std::allocator<std::vector<cell, std::allocator<cell> > >]'
h3main.cpp:9:   instantiated from here
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_construct.h:81: error: cannot allocate an object of type `cell'
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_construct.h:81: error:   since type `cell' has abstract virtual functions
make[2]: *** [build/Debug/Cygwin-Windows/h3main.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2


Basically, what I am trying to do is create a grid of cells, each cell has a coordinate which is also its index in the 2D vector.
I also cross-checked my code with http://www.cplusplus.com/forum/articles/7459/.

I'm pretty sure what's wrong is that class cell has pure virtual functions, so that's why its not letting me declare a 2D vector of type cell, but if I try std::vector<cell> grid, no compilation-time errors occur.

Ultimately I want each cell to be a different type, and I have derived several subclasses, so it makes sense to keep the pure virtual methods I currently have pure virtual, is there anyway around this? is there a better way of doing this?

Thanks in advance for your help.
Last edited on
Why would cell::setCoord take two ints by reference?

The compiler is telling what its problem is:
error: cannot allocate an object of type `cell'
error: because the following virtual functions are abstract:
error: virtual void cell::print(std::ostream&)
error: virtual cell_type cell::getType()

Why would cell::setCoord take two ints by reference?
Take two ints: coord is of type point that has an int x and int y. By reference: just because I didn't want to copy over the original ints, I didn't have to do pass by reference.

Yes, I understand that you can't instantiate abstract base classes, that's why I explained my situation and asked if there was a better way to set it up.
Last edited on
Topic archived. No new replies allowed.