1

I have a class in which I want to create a bitstring from the boost library as a public member of this class. But I get a lot of different errors which I do not understand and want to solve. The following code does compile without errors. I know that the bits are not random, but that doesn't matter yet.

#include <boost/dynamic_bitset.hpp>
class individual{
public: 
    individual(uint32_t bitStringLength):
    bitStringLength_(bitStringLength){}

    void createRandomIndividual(){
        boost::dynamic_bitset<> gene_(bitStringLength_);

        return;
    }

private:
    uint32_t bitStringLength_;
};

But now if I change my code into

class individual{
public: 
    individual(uint32_t bitStringLength):
    bitStringLength_(bitStringLength){}

    boost::dynamic_bitset<> gene_(bitStringLength_);

private:
    uint32_t bitStringLength_;
};

my code won't compile and I get the error:

‘bitStringLength_’ has not been declared boost::dynamic_bitset<> gene_(bitStringLength_);

I do not understand why my code gives me this error. Why can't I generate my bitstring in this way?

samuelnj
  • 1,627
  • 1
  • 10
  • 19
  • 1
    Because you don't get to invent any old syntax you want to? –  May 18 '18 at 21:48
  • I don't understand your comment. I don't understand why it should be declared within a function and not just inside the class directly – DeanTheMachine May 18 '18 at 21:50
  • boost::dynamic_bitset<> gene_(bitStringLength_); You cannot use a member like bitStringLength_ in the class declaration. You need to initialize gene_ in the constructor for example. – tangoal May 18 '18 at 21:51
  • Because that's the way the language is defined. –  May 18 '18 at 21:53
  • @NeilButterworth but why does it then work in my function? I just want to create a boos::dynamic_bitset of length bitStringLength_ without calling a function, since all my individuals should have a gene_ of type dynamic_bitset – DeanTheMachine May 18 '18 at 21:54
  • @tangoal could you explain how I should do it then, since I don't understand why I cannot use a member of my class Individual in this class to create a bitstring of a certain length. How can I initialize gene_ in the constructor then? – DeanTheMachine May 18 '18 at 21:56
  • As I said, that's the way the language is defined. Similarly, you coul not write a for-loop inside a class - it would have to be inside a function. –  May 18 '18 at 21:57
  • @DeanTheMachine: Similar as you did in your first code snippet, but moving it into the constructor of the class: individual(uint32_t bitStringLength): bitStringLength_(bitStringLength) { boost::dynamic_bitset<> gene_(bitStringLength_); } – tangoal May 18 '18 at 22:00
  • but if I for instance would add something like uint32_t x = 2 * bitStringLength_; at the same place as the gene_ then I do not get an error and I don't understand the difference. Do you perhaps have some good resource about classes, constructors and how to define member. I tried the book The C++ programming language by Stroustrup but I do not find it very clear – DeanTheMachine May 18 '18 at 22:01
  • Looks like you're looking for `individual(uint32_t bitStringLength): bitStringLength_(bitStringLength), gene_(bitStringLength){}`. – user4581301 May 18 '18 at 22:04
  • 1
    One of your problems is this: [Why can in-class initializers only use = or {}?](https://stackoverflow.com/questions/16329962/why-can-in-class-initializers-only-use-or) – Bo Persson May 18 '18 at 22:06
  • @tangoal Okay thanks, I don't think I understand the difference really but at least this seems a solution. Why can't I create public members from variables of the initialization? – DeanTheMachine May 18 '18 at 22:08
  • I believe until C++ 11 it was required that all members be instantiated in the manner `class(parameters) : member1(argument), member2(argument)` and only since C++ 11 has the `= and {}` instantiation been allowed. – samuelnj May 18 '18 at 22:08
  • @DeanTheMachine `public` has nothing to do with the initialization problem you're having, it could be a `private` member and you would end up with the same problem. – samuelnj May 18 '18 at 22:11
  • And whichever way you use to initialize the members, they are always initialized from top to bottom. So you have to put them in a proper order. – Bo Persson May 18 '18 at 22:12
  • @BoPersson just to be sure, I read the post and the problem with with way I did it is that it uses () instead of {} and this is not allowed to make sure there is no confusion between functions and expressions? – DeanTheMachine May 18 '18 at 22:14
  • 1
    @Dean - Yes. For class members `T f(x);` is always a function and `T f{x};` is always a member variable. No mixing allowed. – Bo Persson May 18 '18 at 22:22

0 Answers0