Solved

Vector class problem

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
convert char array to number in c 5 83
Copying WordPress Pages 5 77
thread-safe code in c++ 2 86
Visual Studio 2013 and Eclipse Neon 6 13
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

863 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

25 Experts available now in Live!

Get 1:1 Help Now