Buster
Buster

Reputation: 57

c++ bitset error in class

I'm trying to make a floodfill algorithm in C++, so my plan was to make an 2D array, and then each item would be an object, with property's to check for walls around it.

I note walls the following way, 1 = top 2 = right 4 = bottem 8 = left

so 13 would mean, top/bottem/left wall

but when i use std::bitset<sizeof(int)> bits(w); i get a "expected ';' before '(' token How do i fix this? I've tried many things but it just won't work.

My code:

// een simpele floodfill

#include <stdlib.h>
#include <iostream>
#include <bitset>

class Hokjes {
  int x;
  int y;
  int w;



public:
  void  setX(int i) { x = i;      }
  void  setY(int i) { y = i;      }
  void  setW(int i) { w = i;      }
  int   getX()      { return x;   }
  int   getY()      { return y;   }
  int   getVal()    { return x*y; }
  std::bitset<sizeof(int)>   bits(w);   // error happens in this line

  /* FreeSpace? */
  bool  Left()      { 
                      if (bits[3]) return false;
                      return true; }
  bool  Bottum()    {
                      if (bits[2]) return false;
                      return true; }
  bool  Right()     {
                      if (bits[1]) return false;
                      return true; }
  bool  Top()       {
                      if (bits[0]) return false;
                      return true; }           

};

int main() {
using namespace std;

int NrKamers;

std::cin >> NrKamers;

for (int i = 0; i<NrKamers; i++){

    int SizeX;
    int SizeY;
    Hokjes veld[SizeY][SizeX];

    std::cin >> SizeY >> SizeX;

    for (int Yas = 0; Yas <= SizeY; Yas++){
        for (int Xas = 0; Xas <= SizeX; Xas++){
            veld[Yas][Xas].setW((Yas+1)*Xas)
            veld[Yas][Xas].setX(Xas);
            veld[Yas][Xas].setY(Yas);
            std::cout << veld[Yas][Xas].Top() << std::endl;
        }
    }

    std::cout << SizeY << " " << SizeX;
}    


std::cout << NrKamers << std::endl;


system("pause");
return 0;

}

Upvotes: 1

Views: 4139

Answers (2)

Eugen Constantin Dinca
Eugen Constantin Dinca

Reputation: 9150

  1. You're not allowed to initialize member vars at declaration point. So just declare bits: std::bitset<sizeof(int)> bits;
  2. Since it looks like you want to keep w and bits in sync just do bits = i; in the SetW method (or even loose the w member altogether and work directly on bits).

Upvotes: 0

tp1
tp1

Reputation: 288

This will work:

std::bitset<sizeof(int)>   bits;

You'll need to initialize it in constructor:

Hokjes::Hokjes() : bits(w) { }

Upvotes: 3

Related Questions