Solved

Vector class problem

Posted on 2004-03-20
5
681 Views
Last Modified: 2013-12-14
Hi

I have declared :

class Pixel
{
      byte R;
      byte G;
      byte B;
public:
      Pixel(){};
      Pixel(const Pixel&);
      void operator=(const Pixel&);
      friend class Frame;
      friend class Macroblock;
      friend class BMPHeader;
};


class Frame
{
      BMPHeader bmph;
      vector <Macroblock> mblk;
      vector <Pixel> pix;
      int vertmac;
      int hormac;
public:
      Frame(){};            //----------------do nothing constructor
      Frame(BMPHeader &,byte *);
      void createMBlk(void);
      void findMV(Frame &);
      void display(void);
      friend class Video;
};

Frame::Frame(BMPHeader& bmp,byte *buffer)
{
      bmph = bmp;
      pix.reserve(bmph.height*bmph.width);
      for (int j=(bmph.height-1);j>=0;j--)
      {
            for(int i=0;i<bmph.width;i++)
            {
                  int index;
                  switch(bmph.bitsperPixel)
                  {
                        case 24:pix[j*bmph.width+i].R = *buffer++;
                                    pix[j*bmph.width+i].G = *buffer++;
                                    pix[j*bmph.width+i].B = *buffer++;
                                    break;
                        case 16:return;
                                    index = GetWord(buffer);                                                                                                                       buffer += 2;
                                    pix[j*bmph.width+i] = bmph.palette[index];
                                    break;
                        case 8 :index = *buffer++;
 
                                    pix[j*bmph.width+i].R = bmph.palette[index].R;   //problem is here
                                    pix[j*bmph.width+i].G = bmph.palette[index].G;  
                                    pix[j*bmph.width+i].B = bmph.palette[index].B;
                                    break;
                        default:cout << "Color depth not supported";
                                    return;
                  }
            }
            if (bmph.width%4 != 0)
                  for(int i=4;i>bmph.width%4;i--)
                        buffer++;
      }
}



I get error: Access violation unhandled exception. Using VC++.

bmph.palette is also a vector of Pixel class

vector <Pixel> palette;

Please help. It is urgent.

Dhyanesh


0
Comment
Question by:dhyanesh
  • 2
  • 2
5 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 10643129
pix.reserve(bmph.height*bmph.width);

Use size instead of reserve. reserve doesn't add members to vector.

pix.size(bmph.height*bmph.width);

0
 
LVL 4

Author Comment

by:dhyanesh
ID: 10643177
AlexFM

The program does not compile.

It says

C:\Program Files\Microsoft Visual Studio\MyProjects\finalprj\Ver1.cpp(61) : error C2660: 'size' : function does not take 1 parameters

Also size() returns the size of the vector. I dont think it adds members.

Dhyanesh
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 300 total points
ID: 10643200
Sorry, it was typo:

pix.resize(bmph.height*bmph.width);
0
 
LVL 30

Assisted Solution

by:Axter
Axter earned 200 total points
ID: 10643859
A more optimize method would be to define the size in the initialize list.
Example:
Frame::Frame(BMPHeader& bmp,byte *buffer)
: pix(bmph.height*bmph.width)
{
    bmph = bmp;
    //pix.resize(bmph.height*bmph.width);  //Now you don't need this

By declaring the size on the initialize list, the std::vector object gets initialize with the correct size right from the start.
0
 
LVL 4

Author Comment

by:dhyanesh
ID: 10644703
Thank you for your solution AlexFM.

Axter, I like your method as well but AlexFM was first with solution so I give him more points.

Dhyanesh
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

831 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