Solved

Vector class problem

Posted on 2004-03-20
5
692 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to use and create new code templates 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.

751 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