0

This is a continuation of a previous discussion here... I'm trying to convert an existing GDI+ practice program to use for the various GDI+ objects...

So the existing (working) code is this:

   Bitmap *pbitmap = new Bitmap(L"images.png");
   nWidth  = pbitmap->GetWidth();
   nHeight = pbitmap->GetHeight();

Based on input in a recent thread here, I tried this: std::vector<Bitmap*> pbitmap = new Bitmap(L"images.png");

Okay, I already know this is not correct; I don't need to use 'new' with vector, but that immediately poses the question: how do I input the filename into the vector??

I tried:

   std::vector<Bitmap*> pbitmap ;
   pbitmap.push_back(L"images.png"); // HAH!! This doesn't even *begin* to look
                                     // sensible, and indeed, it is not
   nWidth  = pbitmap->GetWidth();
   nHeight = pbitmap->GetHeight();

and neither of these assignments are valid either:

example1.cpp:146:21: error: base operand of '->' has non-pointer type 'std::vector<Gdiplus::Bitmap*>'
  146 |    nWidth  = pbitmap->GetWidth();
      |                     ^~
example1.cpp:147:21: error: base operand of '->' has non-pointer type 'std::vector<Gdiplus::Bitmap*>'
  147 |    nHeight = pbitmap->GetHeight();

I spent a fair bit of time searching the web for pages discussing using with gdiplus, but I don't see anything even remotely useful... So please enlighten me here...

Just for background on me, I have almost 40 years experience with C, mostly in embedded environments, but although I've been using C++ for a decade or so now, it has mostly been "C++ as a better C", as they used to say in Dr Dobbs Journal... it is only recently that I'm trying to actually adopt the correct visions...

8
  • 3
    A vector is not a Bitmap. Commented May 5 at 22:32
  • 2
    Just a quick comment (sorry for not following any of your previous questions), GDI+ is quite dated and isn't very suitable for modern C++ usage, because, IIRC, it requires dynamic allocation of its objects, overloads operator new / delete, but at the same time doesn't have virtual destructors in its base classes (thus breaking the usual polymorphic usage). However, perhaps try std::vector<std::unique_ptr<Gdiplus::Bitmap>> my_vec; — this creates an (initially empty) vector of smart pointers to Bitmap-objects. And then my_vec.push_back(std::make_unique<Gdiplus::Bitmap>(L"images.png")); Commented May 6 at 2:16
  • 1
    (The usage would then be nWidth = my_vec[0]->GetWidth(); nHeight = my_vec[0]->GetHeight();.) By using a smart pointer, you don't need to deal with manual memory management: the Bitmap object will be automatically deleted when the smart poiner will get destroyed, i.e., either by manually removing it from the vector by doing my_vec.pop_back() or just when the vector itself will naturally "go out of scope" (and thus automatically destroy all of its elements). Commented May 6 at 2:23
  • Why are you even refactoring such old code? (assuming it works now) Commented May 6 at 5:34
  • "pbitmap.push_back(L"images.png"); - that makes no sense. You have a container of Bitmap* (pointers to Bitmap) and you are trying to put a string into that container. I'm curious as to what led you to think that that makes sense..? Commented May 6 at 10:58

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.