Bitset in a class

Oct 17, 2013 at 11:21pm
I've declared and initialized a bitset in main with no problems, however when I use the same syntax in a class I get compiler errors.

The following code compiles with no errors:
1
2
3
4
5
6
7
#include <bitset>

int main(int argc, char** argv) 
{
	const std::bitset<4> DIVISOR (std::string("0011"));
	return 0;
}


This code also compiles without errors:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <bitset>
#include <string>

class CRC
{
	private:
		const std::bitset<4> DIVISOR; // (std::string("0011"));
}
;

int main(int argc, char** argv) 
{
	CRC myCRC;
	return 0;
}


However, this code produces compilation errors:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <bitset>
#include <string>

class CRC
{
	private:
		const std::bitset<4> DIVISOR (std::string("0011"));
}
;

int main(int argc, char** argv) 
{
	CRC myCRC;
	return 0;
}


The error says, "[Error] expected ')' before string constant".

Why would the same syntax work in main but not in a class?
I've tried getting rid of the private and const keywords, but the error persists. Any suggestions would be greatly appreciated.
Oct 17, 2013 at 11:33pm
It'll work if you set it in the default constructor. If I remember correctly, you can't set or instantiate class variables in C++.
Oct 17, 2013 at 11:58pm
closed account (Dy7SLyTq)
It'll work if you set it in the default constructor.

that doesnt make any sense. a default constructor is one the compiler generates if you dont have a constructor, so you can't place code in it. as to op: what you need to do is this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <bitset>
#include <string>

class CRC
{
	private:
		const std::bitset<4> DIVISOR;

	public:
		CRC() : DIVISOR(std::string("0011")) {}
}
;

int main(int argc, char** argv) 
{
	CRC myCRC;
	return 0;
}


in c++11 you can initialize class members at declaration with the = operator, but you can't "call" a constructor like that
Oct 18, 2013 at 12:02am
Oct 18, 2013 at 12:09am
@DTSCode: Your code worked great. Thanks a lot. As to the syntax you used: is that a shorthand way of defining the constructor?

I've actually heard people call a constructor without parameters a default constructor--I assumed this is what GRex was referring to.
Last edited on Oct 18, 2013 at 12:25am
Oct 18, 2013 at 12:15am
I tried this first, but it did not work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <bitset>
#include <string>

class CRC
{
	private:
		const std::bitset<4> DIVISOR;

	public:
		CRC();
}
;

CRC::CRC()
{
	  DIVISOR (std::string("0011"));
}

int main(int argc, char** argv) 
{
	CRC myCRC;
	return 0;
}
Last edited on Oct 18, 2013 at 12:16am
Oct 18, 2013 at 12:19am
Although, this works:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <bitset>
#include <string>

class CRC
{
	private:
		const std::bitset<4> DIVISOR;
	public:
		CRC();
}
;

CRC::CRC() : DIVISOR (std::string("0011"))
{
	
}

int main(int argc, char** argv) 
{
	CRC myCRC;
	return 0;
}
Oct 18, 2013 at 12:22am
Okay, so the colon is used for initialization.
Oct 18, 2013 at 12:26am
closed account (Dy7SLyTq)
@naraku&GRex: my bad. i wasnt aware c++ had its own meaning. i thought it was the oop def

@op: yes. it calls the the constructor at declaration. the reason this http://www.cplusplus.com/forum/beginner/113905/#msg622066 doesnt work is because constructors can only be called upon the declaration of the variable, not willy nilly. the reason why http://www.cplusplus.com/forum/beginner/113905/#msg622067 works is because you essentially wrote the same thing i did, except you did it outside of the class, which is useful when you have large methods. there might be other performance issues, but thats the major one imo
Oct 18, 2013 at 12:35am
@DTSCode: Nice. Thanks for all the info. I definitely learned something tonight.

Thanks for all the help everyone.
Topic archived. No new replies allowed.