Solved

bitset::to_string()  won't compile with g++

Posted on 2001-07-24
9
578 Views
Last Modified: 2012-08-13
i'm in linux and trying to use the bitset::to_string() function to convert a bitset to a string.  but g++ keeps giving me these errors saying that it doesn't recognize the function.  is there anything else i need to include besides <bitset>?

_ryan
0
Comment
Question by:tetsuo
9 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 6315829
Are you using the proper namespace?

Can you show your code?
0
 
LVL 30

Expert Comment

by:Axter
ID: 6315831
<bitset> should be the only include you need.
0
 

Author Comment

by:tetsuo
ID: 6315958
i tried this:

#include <bitset>

using std::to_string;

but it didn't help the problem.  what is the proper namespace i should use?

_ryan

i'll post code shortly, i cannot get to it right now..
0
 

Author Comment

by:tetsuo
ID: 6316348
here is the code:

/******************************************************************************
 * lz.h - header file for all lz components
 ******************************************************************************/

#include <cmath>

#include <vector>

using std::size;
using std::begin;
using std::end;

#include <iostream>

using std::cout;
using std::cin;
using std::cerr;

#include <fstream>

using std::ifstream;
using std::good;
using std::read;
using std::write;

#include <string>

using std::string;
using std::append;

#include <bitset>

using std::to_string;

#include <sstream>

using std::sstream;
using std::itostring;

class LZParser  {

    public:

        LZParser();
        void parse();  //performs LZ parsing on messageVector
        void printMessageVector();   //what do you think??
        void printBlockVector();     //ditto
        void printAlphabet();        //ditto

        void getBlockVector(vector<string> & b);

        bool checkFor(string word);  //checks for previous occurrence
                                     //of currentWord in blockVector

        void readFile(string filename);   //read in the data file
                                          //and set the alphabet.

        void readBinary (string filename);   //read in the data file
        friend istream & operator>>(istream & is, string & st);

    private:

        vector<string> alphabet;
        vector<string> messageVector;
        vector<string> blockVector;

};

class LZEncoder {

    public:

        LZEncoder();

        void lzEncode();
        void createPairs(vector<string> &blocks);
        void printPairs();

    private:

        vector<string> pairs;

};

/******************************************************************************
 * lzparser.cxx - impelentation of a lzparser
 ******************************************************************************/

#include "lz.h"

LZParser::LZParser ()
{

}

void LZParser::parse()
{

    string currentWord = "";
    int messageIndex = 0;
    bool done = false;

    currentWord = messageVector[0]; //first block is X1
    cout<<"first word is:::: "<<currentWord<<"\n";
//    messageIndex++;

    while (messageIndex < messageVector.size()) {
        done = false;
        currentWord = "";

        //this inner loop constructs the next shortest prefix not
        //already contained in the block vector.

        while (!done && messageIndex < messageVector.size()) {
            currentWord.append(messageVector[messageIndex]);
            messageIndex++;

            if (!checkFor(currentWord)) {
                cout << "inserting *" <<currentWord<< "* into blockVector\n";
                blockVector.insert(blockVector.end(), currentWord);
                done = true;
            }
            //if the word is already contained but we are at the end,
            //just add the word to blockVector, (Edited by Computer101) it.
            else if (messageIndex >= messageVector.size() - 1) {
                blockVector.insert(blockVector.end(), currentWord);
            }
        }
    }
}

void LZParser::getBlockVector(vector<string> & b)
{
    b = blockVector;
}


//check for this word in blockVector
bool LZParser::checkFor(string word)
{
    string w = "";
    int i;

    if (blockVector.size() <= 0) {
        return false;
    }

    for (i = 0; i < blockVector.size(); i++ ) {
        if (blockVector[i] == word) {
            return true;
        }
    }
    return false;

}

void LZParser::printMessageVector()
{

    for (int i = 0; i < messageVector.size(); i++) {
        cout<<messageVector[i]<<" ";
    }

    //cout<<"\nDONE!\n";
}

void LZParser::printBlockVector()
{
    for (int i = 0; i < blockVector.size(); i++) {
        cout<<blockVector[i]<<" ";
    }

    //cout<<"\nDONE!\n";
}

void LZParser::printAlphabet()
{
    for (int i = 0; i < alphabet.size(); i++) {
        cout<<alphabet[i]<<" ";
    }

    //cout<<"\nDONE!\n";
}

void LZParser::readFile(string filename)
{
    bool contained = false;
    int k;

    ifstream inFile (filename.c_str(), ios::in);

    if (! inFile.good() ) {
        cerr << "Input file could not be opened!\n";
        exit(1);
    }

    string str;

    while (inFile>>str) {
        contained = false;
        messageVector.insert(messageVector.end(), str);
        for (k = 0; k < alphabet.size(); k++) {
            if (str == alphabet[k]) {
                contained = true;
            }
        }

        if (!contained) {
            alphabet.insert(alphabet.end(), str);
        }
    }

    //cout<<"Finished constructing message vector.\n";
}

void LZParser::readBinary (string filename)
{

    ifstream in (filename.c_str(), ios::in | ios::binary);

    string mess = "";
    string temp = "";
    char c[1];
    unsigned long b;
    bitset<8> bs;

    for (int i = 0; i < 13; i++) {
        in.read(c, 1);
        b = c[0];
        bs = b;
        cout<<bs<<'\n';
        temp = bs.to_string();
        mess.append(temp);
    }

    cout<<"\nmess is: "<<mess<<"\n";
    in.close();
}


istream & operator>>(istream & is, string & st) {
    char temp;
    is.get(temp);
    if (is)
        st = temp;
    return is;
}

#include "lz.h"

std::string itostring(int i)
{
  std::ostringstream os; // Note no length restrictions on the data.
  os << i;
  return os.str();
}




LZEncoder::LZEncoder()
{

}

void LZEncoder::lzEncode()
{

}

void LZEncoder::createPairs(vector<string> &blocks)
{
    cout<<"block vector output without iteration..\n";
    cout<<"now creating pairs...\nreceiving blockVector from lzparser...\n";
/*
    for (int x = 0; x < blocks.size()-1; x++) {
        cout<<"blocks["<<x+1<<"] = "<<blocks[x]<<"\n";
    }
*/
    int x = 0;
    while (x < blocks.size()) {
        cout<<"blocks["<<x+1<<"] = "<<blocks[x]<<"\n";
        x++;
    }


    cout<<"\n";



    int i, k;

    string blockNum = "0", concat = "";
    string currentBlock = "";

    //first pair
    pairs.insert(pairs.end(), blockNum);   //i'm following ali's notes here.
                                           //i don't think the ordering
                                           //matters, as long as it's
                                           //consistent.
    pairs.insert(pairs.end(), blocks[0]);

    //parse each variable length block currentBlock and try to
    //find a previous block of length currentBlock.length()-1 that
    //matches the subblock of the same size of the currentBlock.
    //if found, that is the block number of the current pair,
    //and concatenation is currentBlock[currentBlock.length()-1],
    //(the last character of the current block)

    for (i = 1; i < blocks.size(); i++) {
        currentBlock = blocks[i];  //get current block
        concat = currentBlock[currentBlock.length()-1];  //get last char

        currentBlock.erase(currentBlock.length()-1);  //erase last char to
                                                      //create sub block.
        cout<<"\nblock is:"<<blocks[i]<<"\n";
        cout<<"concat is: "<<concat<<"\nsearching for "<<currentBlock<<"\n";

        for (k = 0; k < i; k++) {
            if (blocks[k] == currentBlock) {
                cout<<"FOUND: "<<blocks[k]<<"\n";
                blockNum = itostring(k+1);
                break;
            }
        }
        cout<<"inserting blockNum: "<<blockNum<<"\n";
        cout<<"inserting concat: "<<concat<<"\n";

        pairs.insert(pairs.end(), blockNum);
        pairs.insert(pairs.end(), concat);

    }


}

void LZEncoder::printPairs()
{
    for (int i = 0; i < pairs.size(); i++) {
        cout<<"("<<pairs[i]<<','<<pairs[i+1]<<") ";
        i++;
    }
    cout<<'\n';
}


/* end of code */


i'm sorry if the format is all screwy, this text box is not so big.  so am i not including something?  i've tried looking up standard template library references and i don't know what i'm doing wrong.  any help is appreciated, i will make sure that ALL who help will get the acknowledgement they deserve!  thanx

_ryan

p.s.  i will clarify any of my code upon request  ;)
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 30

Expert Comment

by:Axter
ID: 6316933
>>using std::to_string;

That is not the right namespace.
using std::bitset;//Correct
0
 

Author Comment

by:tetsuo
ID: 6318667
0
 

Author Comment

by:tetsuo
ID: 6318829
that didn't work.  here is the error i get:

lzparser.cxx: In method `void LZParser::readBinary (basic_string<char,
string_char_traits<char>, __default_alloc_template<true, 0> >)':
lzparser.cxx:150: no matching function for call to `bitset<8, long
unsigned int>::to_string ()'
make: *** [lzparser.o] Error 1
0
 
LVL 11

Expert Comment

by:griessh
ID: 6833180
I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. Unless there is objection or further activity,  I will suggest to refund the points and PAQ at zero points since nobody had a satisfying answer for you.

The link to the Community Support area is: http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
LVL 5

Accepted Solution

by:
Netminder earned 0 total points
ID: 6853166
Per recommendation points refunded and question closed.

Netminder
Community Support Moderator
Experts Exchange
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now