0

I have two classes:

  • buffer, which allocates char buffer

  • file, which manages file io

My file.read() functions wants to return a buffer object, but i cannot assign it to a variable (see main() at the end of the code is where the error is))

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


class buffer {
public:
    buffer(int _size) : buf((char *)malloc(_size)), len(_size)
    {
    }

    char *ptr()
    {
        return buf;
    }

    int size()
    {
        return len;
    }

    ~buffer()
    {
        free(buf);
    }

    buffer() : buf(0), len(0)
    {
    }

    buffer(buffer& rhs)
    {
        buf = (char *)malloc(rhs.len);
        memcpy(buf, rhs.buf, rhs.len);
    }

    operator=(buffer& rhs)
    {
        free(buf);
        buf = (char *)malloc(rhs.len);
        memcpy(buf, rhs.buf, rhs.len);
    }
private:
    char *buf;
    int len;
};

class file {
public:
    file(const char *filename, const char *mode) : f(fopen(filename, mode))
    {
    }

    int size()
    {
        int ret;
        int pos = ftell(f);
        fseek(f, 0, SEEK_END);
        ret = ftell(f);
        fseek(f, 0, pos);
        return ret;
    }

    buffer read(int n)
    {
        buffer buf(n);
        fread(buf.ptr(), 1, n, f);
        return buf;
    }

    void write(buffer& buf)
    {
        fwrite(buf.ptr(), 1, buf.size(), f);
    }

    ~file()
    {
        fclose(f);
    }
private:
    FILE *f;
};

int main(int argc, char *argv[])
{
    if (argc == 3) {
        file infile(argv[1], "rb");
        file outile(argv[2], "wb");
        buffer buf = infile.read(infile.size());
    }
}

Please ignore that the code is not generic (no templates and poor error handling) this is just a demo I am learning c++.

Update

Still not working:

buffer& operator=(const buffer& rhs)
{
    free(buf);
    buf = (char *)malloc(rhs.len);
    memcpy(buf, rhs.buf, rhs.len);
}


file infile(argv[1], "rb");
file outile(argv[2], "wb");
buffer buf = infile.read(infile.size()); // error
J. Doe
  • 73
  • 9
  • Can you reduce that to a [MCVE] please. – πάντα ῥεῖ May 26 '17 at 09:23
  • 1
    The correct signature for the assignment operator is `buffer& operator=(const buffer& rhs)` – πάντα ῥεῖ May 26 '17 at 09:25
  • i copied the assignment operaotr signature from this code: https://stackoverflow.com/questions/3278625/when-do-we-have-to-use-copy-constructors which is then obviously wrong and it was accepted as RIGHT answer. – J. Doe May 26 '17 at 09:29
  • Very nice that people are accepting wrong answers as right and me as a newbie I use that "already answered questions" and they do it again, they redirect users to the wrong answers. – J. Doe May 26 '17 at 09:36
  • That an answer is accepted merely means nothing. Also you copied it wrong. At least you missed the return type `void` (which is also legal but not a good idea). I redirected you to the correct answers. Everthing you need to know about operator oveloading. The answer you're looking for is [here](https://stackoverflow.com/a/4421719/1413395). – πάντα ῥεῖ May 26 '17 at 09:37
  • As mentioned provide a [MCVE] and post your error messages verbatim, otherwise no one can help you here. You didn't spot the **minimal** obviously. There's a lot of code posted that can be thrown out and has nothing to do with your problem I'm sure. – πάντα ῥεῖ May 26 '17 at 09:42
  • Still not working properly with the fixed assignment operator. This works: `buffer buf; buf = ......' but this does not: `buffer buf = ....` – J. Doe May 26 '17 at 09:44
  • Read about initialization vs assignment. You need to provide a copy constructor for the latter case. Nothing to do with assignment. And your copy constuctor seems to have the same problem, signature should be `buffer(const buffer& rhs)` – πάντα ῥεῖ May 26 '17 at 09:45
  • Hint: `buffer buf = infile.read(infile.size());` is *not* assignment -- it's copy construction. – G.M. May 26 '17 at 09:46
  • Thanks but I dont want to read 20 pages just to find out that I missed a `const' keyword. Thanks anyway, finally. Non of my variables was const except the literal strings. C++ is a mess. Thank you Brian Stroustroup for inventing this messy language. – J. Doe May 26 '17 at 09:50
  • @J.Doe Who's _Brian Stroustroup_? – πάντα ῥεῖ May 26 '17 at 09:57

0 Answers0