SOLVED: Problem with bitset

Aug 14, 2008 at 11:05am
Hi all, (yet another) problem saw it fit to grace me with its presence...

I'm trying to use the bitset container, but somehow I must be using the wrong libraries, for I get an undeclared identifier compiler error when trying to compile the following code;

in filter.hpp:

1
2
3
4
5
6
7
8
#include <stdlib.h>
#include <iostream>
#include <bitset>
#include <string>

void getVGFz(char *, long);

//stuff 

and in filter.cpp:

1
2
3
4
5
6
7
8
9
10
#include "filter.hpp"

//more stuff

void getVGFz(char *buffer, long linecount){
    //yet more stuff
    char VGbuffer[] = "0101";
    std::bitset<4> VGbits(string(VGbuffer));
    long value = VGbits.to_ulong();
}

I get the error
'string' undeclared (first use this function)
on line 9 of the second code excerpt. Am I using 'string' in a bad way here (I looked at the bitset entry in the site's documentation)? I have the feeling I need to initialize VGbuffer not as an array of type char but as an object of type string.

Thanks in advance for your help :)
Last edited on Aug 18, 2008 at 8:50am
Aug 14, 2008 at 12:03pm
string is defined in the std namespace.
Aug 14, 2008 at 12:41pm
That made it work, but...

Now it objects to the use of VGbits.to_ulong().

I feel a bit like Rocky in the chicken pie machine.
Last edited on Aug 14, 2008 at 12:45pm
Aug 14, 2008 at 12:59pm
closed account (z05DSL3A)
bitset::to_ulong returns an unsigned long with the integer value that has the same bits set as the bitset.

Aug 14, 2008 at 1:08pm
Gah. Trees blocking my sight of the woods. Sorry.

Edit: But why does it tell my that VGbits is of non-class type and refuse compiling? I know it is of non-class type, I instatiated an object VGbits of the type bitset, did I not? I think I used a direct adaptation of the example on the site.
Last edited on Aug 14, 2008 at 1:19pm
Aug 14, 2008 at 1:32pm
closed account (z05DSL3A)
try:
1
2
3
4
5
6
7
void getVGFz(char *buffer, long linecount)
{
    //yet more stuff    
    std::string VGbuffer = "0101";
    std::bitset<4> VGbits(VGbuffer);
    unsigned long value = VGbits.to_ulong();
}
Last edited on Aug 14, 2008 at 1:32pm
Aug 14, 2008 at 1:56pm
I think there is something wrong with my compiler, for I put this in a clean, new project:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <cstdlib>
#include <iostream>
#include <bitset>
#include <limits>

using namespace std;

void dosomething()
{
    string buffer = "1010";
    bitset<4>bits(string(buffer));
    unsigned long value = bits.to_ulong();
    cout << value << endl;
}

int main(int argc, char *argv[])
{
    dosomething();
std::cout << "Press ENTER to continue...";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return 0;
}


(Thanks to Duoas for the pause code)

It still gives me this error:

In function 'void dosomething()':
request for member 'to_ulong' in 'bits', which is of non-class type 'std::bitset<4u>()(std::string)'
[Build Error] [main.o] Error 1

(Using Dev-C++ 4.9.9.2)

I don't mean to be a blockhead, but I just can't see what I'm doing wrong.
Last edited on Aug 14, 2008 at 3:06pm
Aug 14, 2008 at 1:59pm
closed account (z05DSL3A)
Change line 11 to bitset<4> bits(buffer);, it should be fine.
Aug 14, 2008 at 3:17pm
Well, I'm home from work now (trainee... though why they assigned me to a C++ programming task, I do not know, I suck at it, obviously) and won't be back there until monday, but trying to compile it on my mac (gcc 4.0.1) caused a whole slew of errors to crop up, some examples:

Undefined symbols:
"std::basic_ostream<char, std::char_traits<char> >::operator<<(unsigned long)", referenced from:
dosomething() in ccrWe5hQ.o
"___gxx_personality_v0", referenced from:
___gxx_personality_v0$non_lazy_ptr in ccrWe5hQ.o
"std::allocator<char>::allocator()", referenced from:
dosomething() in ccrWe5hQ.o
"std::basic_ostream<char, std::char_traits<char> >& std::endl<char,
std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)", referenced from:
__ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_$non_lazy_ptr in ccrWe5hQ.o


It all looks a bit strange to me, but maybe switching from Win XP to Mac OS X is the problem.
Last edited on Aug 14, 2008 at 3:29pm
Aug 14, 2008 at 3:45pm
closed account (z05DSL3A)
I have compiled the following on both XP and Fedora, there should not be any problem with this code on a Mac.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <cstdlib>
#include <iostream>
#include <bitset>
#include <limits>

using namespace std;

void dosomething()
{
    string buffer = "1010";
    //bitset<4>bits(string(buffer)); 
    bitset<4> bits(buffer);
    unsigned long value = bits.to_ulong();
    cout << value << endl;
}

int main(int argc, char *argv[])
{
    dosomething();
    std::cout << "Press ENTER to continue...";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    return 0;
}
Aug 14, 2008 at 7:20pm
Then something is wrong with my mac, since it does not compile :(
Aug 14, 2008 at 7:43pm
closed account (z05DSL3A)
If you are still getting the errors from your previous post, it may help to #include <string> .
Aug 18, 2008 at 6:10am
I'm sorry, I still can't get it to run, even though I tried for quite some time. I'll just roll my own algorithm.

Thank you for the help, though :)

EDIT: It works now, the problem was an unlinked library 9_9

I still have to find out what made the code unwilling to compile on my mac, though.
Last edited on Aug 18, 2008 at 9:12am
Topic archived. No new replies allowed.