Vector bidimensional & class variables


Hi to all. :)

I need to create a bidimensional vector and work on it like a bidimensional array. I don't find any example of code and all the implementations of vectors I've seen are in only one dimension. But if it is one of the most powerful data structures of C++ I think I can make it in 2 dimensions.

For the first time I am at work to make a program with classes.
I've tried to declare an int variable in one class and initialize it in the costructor of the class, but compiler give a linking error:
[Linker error] undefined reference to `number'

I can't set the value of the variable in any part of the program, neither in the constructor, neither in standard metods, neither in the main. :(

I really don't know where is the error:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

class snake{
      public:
             snake(); 
             int stampaSnake(int righe); 
             static const int COLONNE = 4;
             static int number; 
      };

snake::snake()
{ 
int a =0;
int righe = 4;
number=4; //error
}


if I remove the row "number=4;" the program works correctly. But I need this variable. :(

Thanks for reading!! :)

PS. I'm using Dev-C++ 4.9.9.2

Thanks again.
Last edited on
It is because you did not actually create the variable (which you must do) -- you only declared it as existing with the static keyword. On line 9, add:
 
int snake::number;

Then you can compile it.

BTW, you should make sure you understand what it is that you are doing by declaring class data members as "static".

Hope this helps.
Thanks so much, now it works. :D

Static is because at the beginning of program, if I remember correctly, I have written this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19


class snake{
      public:
             snake(); 
             int stampaSnake(int righe); 
             static const int COLONNE = 4;
             int number; 
      };


int main {

snake game;
game.number=3;
cout<<game.number;

return 0;
}


So, the compiler gave me an error like it couldn't compile because number wasn't a static member. I have declared it static and maintained this but, as you know, new errors are incurred :(.

I also read that a static member is visible only in the area where it has been declared. I need it only in the methods of the class, so I think I can mantain it as static (the print in the main has been just a test). :)

No suggestions about bidimensional vector? :(

Thanks :D
For simplicity, just use a vector of vectors. (Sorry to have forgotten about your Q.)

Often, it is worth making your own class to do it. Here is the beginning of a simple "matrix" class you can play with:
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <vector>
using namespace std;

//----------------------------------------------------------------------------
// This is the 'matrix' class.
//
template <typename Number>
struct matrix: vector <vector <Number> >
  {
  matrix( unsigned m, unsigned n, const Number& value = Number() )
    {
    resize( m, vector <Number> ( n, value ) );
    }
  unsigned rows()    const { return this->        size(); }
  unsigned columns() const { return this->front().size(); }
  };

//----------------------------------------------------------------------------
// This is the output operator for the 'matrix' class. Yes, I know it is pretty simple...
//
template <typename Number>
ostream& operator << ( ostream& outs, const matrix <Number> & m )
  {
  for (  unsigned row = 0; row < m.rows();    row++)
    {
    for (unsigned col = 0; col < m.columns(); col++)
      outs << m[ row ][ col ] << " ";
    outs << "\n";
    }
  return outs;
  }

//----------------------------------------------------------------------------
// A quick example of use
//
int main()
  {
  int rows, cols, value;

  cout << "number of rows> ";    cin >> rows;
  cout << "number of columns> "; cin >> cols;
  cout << "initial value> ";     cin >> value;

  matrix <int> M( rows, cols, value );

  cout << "Initial matrix value:\n" << M << endl;

  return 0;
  }

Currently, there is no protection against inappropriate modifications to the matrix -- you can add that kind of stuff if it is needed. Also, you'll notice there is no input for it. All these things are an exercise left to you ;-)

Hope this helps.
Topic archived. No new replies allowed.