• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 608
  • Last Modified:

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

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
tetsuo
Asked:
tetsuo
1 Solution
 
AxterCommented:
Are you using the proper namespace?

Can you show your code?
0
 
AxterCommented:
<bitset> should be the only include you need.
0
 
tetsuoAuthor Commented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
tetsuoAuthor Commented:
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
 
AxterCommented:
>>using std::to_string;

That is not the right namespace.
using std::bitset;//Correct
0
 
tetsuoAuthor Commented:
0
 
tetsuoAuthor Commented:
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
 
griesshCommented:
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
 
NetminderCommented:
Per recommendation points refunded and question closed.

Netminder
Community Support Moderator
Experts Exchange
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now