Solved

Vector class problem

Posted on 2004-03-20
5
662 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now