Solved

Vector class problem

Posted on 2004-03-20
5
684 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Copying WordPress Pages 5 87
Recommendation vb6 to vb.net or others 14 174
Issues with C++ Class 19 100
eclipse apache tomcat admin console 52 120
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

789 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