I just signed in to these forums and I'd be so happy if you guys could help me out with a triviality that I can't seem to get around.

Now, although I haven't actually got around to programming anything really complex, I've been reading alot these last 2 months and I wanted to start a project that'd be a challenge.

I got the main() ready and ofcourse it won't compile if I don't declare any of the three funtions in it, which are the only errors.

I tried declaring one of them in a header file but since it's a function that's supposed to return bitsets and has a vector class in one parameter and another bitset in the other I get all these errors and I can't get it to work.

The header file with the function declaration is this:

#include <string>
#include <bitset>
#include <vector>
#include <iostream>


bitset Read(vector v, bitset b);

The error I get is "`bitset' does not name a type ".

Could someone please help me out with this?

tux4life commented: Code tags on first post, I'm happy to see that :) +9

Dani AI

Generated

A short synthesis and concrete fixes for the compilation messages in this thread. correctly spotted that std::bitset is a class template that needs a compile-time size, and correctly pointed out the std:: namespace. The cryptic errors like "bitset' does not name a type" or "expected constructor... before '<' token" come from leaving the template parameter out: the compiler then treats the<` as a less-than sign and the declaration becomes syntactically invalid.

For a reusable, header-only declaration that works for any compile-time size, make the function a template and fully qualify standard types. Example header pattern:

#ifndef READ_H
#define READ_H

#include <cstddef>
#include <bitset>
#include <vector>
#include <string>

template <std::size_t N>
std::bitset<N> Read(const std::vector<std::string>& v, const std::bitset<N>& b);

#endif // READ_H

Notes on that pattern: use template<std::size_t N> when the bitset size is fixed at compile time; pass containers and bitsets by (const) reference to avoid copies; include the needed headers in the header file (do not rely on transitive includes). Do not put using namespace std; in headers and never try to forward-declare std::bitset inside namespace std — include <bitset> instead.

If a runtime-sized bitset is required, consider std::vector<bool> or boost::dynamic_bitset (from Boost) instead, and change the function signature accordingly. Quick checklist for debugging similar errors: confirm correct headers are included, fully qualify std types in headers, provide template parameters for templates (or make the function a template), and make sure the declaration exactly matches the definition in the .cpp file.

Recommended Answers

All 4 Replies

The error I get is "`bitset' does not name a type ".
Could someone please help me out with this?

I think you've forgot something, check out this :)
(You forgot specifying the number of bits)

but if I do specify the number of bits, I get another error that says "expected constructor, destructor, or type conversion before '<' token " and "expected `,' or `;' before '<' token". I shouldn't have to specify them though, I should be able to declare the type without giving specific parameters to the class.

Also: bitset is part of the std namespace, so it's std::bitset

commented: Haha, yes! +9

Oooooooh, so that's what I was missing.

I have a couple more errors but I'll try to sort them out by myself, it's just that that one was really getting to me. Thanks guys! : D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.