2

I have a class in another .h and .cpp file from my main.cpp.

In main.cpp:

#include <iostream>
#include "filestuff.h"
#include "stream.h" <-- right here

int main(int argc, char** args)
{
    if(!args[1])
        return 0;

    u64 filesize = 0;
    get_file_size(args[1], &filesize);
    u8* fd = new u8[filesize];
    read_file(args[1], fd, filesize);

    Stream s = new Stream(fd, filesize, "rb", BigEndian );

    getchar();
    return 0;
}

In Stream.h

#include <iostream> //just in case?

#if defined(_WIN32) && defined(_MSC_VER)
    typedef __int64 s64;
    typedef unsigned __int64 u64;
#else
    typedef long long int s64;
    typedef unsigned long long int u64;
#endif

typedef unsigned long int u32;
typedef signed long int s32;
typedef unsigned short int u16;
typedef signed short int s16;
typedef unsigned char u8;
typedef signed char s8;

#define LittleEndian 0x1
#define BigEndian 0x2

class Stream
{
public:
    Stream(u8* buffer, u64 length, char* access, int IOType);
private:
    u8* buffer;
    u64 pos;
    u64 len;
};

In Stream.cpp

#include "stream.h"

Stream::Stream(u8* buffer, u64 length, char* access, int IOType)
{
    u8* buf = new u8[length];
    buffer = buf;
}

How can I use the following code to initialize my class like I want to do in main now?

Stream* s = new Stream(fd, filesize, "rb", BigEndian );
default
  • 11,485
  • 9
  • 66
  • 102
MysteryDev
  • 610
  • 2
  • 12
  • 31

5 Answers5

6

Real error is in #define LittleEndian = 0x1. Remove =.

cahn
  • 1,350
  • 1
  • 12
  • 24
2

You have an error here :

Stream *s = new Stream(fd, filesize, "rb", BigEndian );
//     ^

The operator new return a pointer to the newly allocated storage space. So s has to be a pointer to get it working.

And you should have a destructor. You are allocating dynamic memory with new[], you should free it before leaving :

Stream::~Stream()
{
    if ( NULL != buffer )
    {
        delete [] buffer;
    }
}

And when you don't need s anymore :

delete s;

Last error :

#define LittleEndian = 0x1
//                   ^

Remove the =.

And in C++, it is a better practice to use static const variable :

static const int LittleEndian = 0x1;

Look at this : static const vs #define

EDIT : For the constructor :

Stream::Stream(u64 iLength, char* iAccess, int iIOType):
    mBuffer( new u8[iLength] )
{
}

// With mBuffer member of the Stream class.
Community
  • 1
  • 1
Pierre Fourgeaud
  • 14,290
  • 1
  • 38
  • 62
  • better yet I could probably do if(buffer){ buff = new u8[len]; buffer = buff; } – MysteryDev Jul 23 '13 at 06:38
  • I suggest to rename the arguments in your constructor or you members in your class, it is a bit confusing... Look at the edit. – Pierre Fourgeaud Jul 23 '13 at 06:41
  • 1
    the `~` is the caractere for the destructor. It is the method who is called at the destruction of the object. You should read this : http://www.cplusplus.com/doc/tutorial/classes/ – Pierre Fourgeaud Jul 23 '13 at 06:45
0

There are two probable mistakes. If you want a pointer to an object, write

Stream *s = new Stream(fd, filesize, "rb", BigEndian );

And if you want an object itself and not a pointer, write

Stream s(fd, filesize, "rb", BigEndian );
Ivan Smirnov
  • 4,365
  • 19
  • 30
0

in your main if you use dynamic allocation you must replace

     Stream s = new Stream(fd, filesize, "rb", BigEndian );

by

      Stream* s = new Stream(fd, filesize, "rb", BigEndian );

so s become a pointer on a Stream object. all call to function of the stream class should be done with : s-> or (*s).

and don't forget to remove your memory with

 delete s;
alexbuisson
  • 7,699
  • 3
  • 31
  • 44
0

Plenty of errors pointed out so far. Here's another: In your constructor, you set

buffer = buf

That doesn't actually assign to the Stream's buffer member. That assigns to the constructor's buffer argument. You most likely want

this->buffer = buf

That said, I'm not really sure why you take a buffer as an argument, only to allocate a different one. Maybe it'll make sense when you finish your code.

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • no you're right, I noticed that after I posted it. It's 1:40 AM. I really should have attempted this during day time. :/ – MysteryDev Jul 23 '13 at 06:40