0

I am trying to make my first class with a constructor and it seems to be acting strangely. My class is derived from filebuf and for some reason, I am unable to open it in the constructor. I tried to add a cout statement for debugging, but the << operator is not recognized.

#include <iostream>
#include "bin.h"

int main()
{
    bin myBin("e:\Temp\test.txt");


    system("PAUSE");
    return 0;
}

bin.h

#pragma once
#include <fstream>
#include <cstdlib>
#include <cstring>

class bin : private std::filebuf {

int buffSize = 1000;
char* buffer;
unsigned int length;
short int buffCounter;

public:
    bin(std::string fileName)
    {
        open(fileName.c_str(), std::ios::in | std::ios::out | std::ios::trunc);
        if (!is_open())
            std::cout << "ERROR: failed to open file " << fileName << std::endl;

        //set all IO operations to be unbufferred, buffering will be managed manually
        setbuf(0, 0);
        //create buffer
        buffer = new char[buffSize];

    };


     virtual ~bin()
    {
        delete buffer;
    };
};
Lord Zsolt
  • 6,492
  • 9
  • 46
  • 76
Michael
  • 1,263
  • 1
  • 12
  • 28

4 Answers4

2
bin myBin("e:\Temp\test.txt");

You have to correct above line as follows:

bin myBin("e:\\Temp\\test.txt");

DEMO: http://cpp.sh/7b4k

Steephen
  • 14,645
  • 7
  • 40
  • 47
  • I corrected it and I still get the same build error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) c:\users\mt\documents\visual studio 2013\projects\cipher\cipher\bin.h 18 – Michael Jun 13 '15 at 18:02
  • @Person93 please refer the demo, it is working after this change – Steephen Jun 13 '15 at 18:06
1

It looks like you need:

#include <iostream>
donjuedo
  • 2,475
  • 18
  • 28
1

To use std::string you need:

#include <string>

The iostream include may have forward-declared std::string but without the full definition you don't get operator<< (or c_str()).

Some other answerers may be unable to reproduce your problem because different standard libraries might have their iostream fully do #include <string> (this is permitted but not required).

M.M
  • 138,810
  • 21
  • 208
  • 365
-1
std::cout << "ERROR: failed to open file " << fileName << std::endl;

Should be

std::cout << "ERROR: failed to open file " << fileName.c_str() << std::endl;

std::cout doesn't always accept std::string but does accept const char *

user3476093
  • 704
  • 2
  • 6
  • 11
  • I am a little confused over this arguement: http://coliru.stacked-crooked.com/a/4168e5b7507e6e42 – Steephen Jun 13 '15 at 18:12
  • 1
    Yeah this is totally false, `std::string` has an overloaded [`operator<<`](http://en.cppreference.com/w/cpp/string/basic_string/operator_ltltgtgt) – Barry Jun 13 '15 at 18:17
  • @Barry - the reason why I put this answer was because the asker of the question put on a comment to Steephen's answer that 'error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) c:\users\mt\documents\visual studio 2013\projects\cipher\cipher\bin.h 18' which is why I put this answer. – user3476093 Jun 13 '15 at 18:19