CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2004
    Posts
    204

    Declaring an object using the "new" keyword

    The following line of code won't compile (I copied it from MSDN)
    myMetafile = new Metafile(L"MyDiskFile.emf", hdc);

    I get this error:
    error C2065 'myMetafile' : undeclared identifier

    I've never declared anything with new before, so I don't have any ideas as to how to fix this.

    Any ideas?

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Declaring an object using the "new" keyword

    Quote Originally Posted by mmscg View Post
    The following line of code won't compile (I copied it from MSDN)
    myMetafile = new Metafile(L"MyDiskFile.emf", hdc);

    I get this error:
    error C2065 'myMetafile' : undeclared identifier

    I've never declared anything with new before, so I don't have any ideas as to how to fix this.
    What does your C++ book say about "new"? Not only that, what does it say about "delete"?

    You can't just copy stuff out of MSDN without understanding basic C++.

    The "new" returns a pointer, therefore that variable must be a pointer.

    Secondly, you have to call "delete" to deallocate the memory or you get a memory leak.

    Third, programmers try to avoid "new", since it creates programs that are harder to maintain.

    So to be honest, if you don't know what "new" means, I suggest you not use it, or learn why it is used and the drawbacks if used too much.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: Declaring an object using the "new" keyword

    Metafile sounds like a GDI+ class.
    Isn't it?
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  4. #4
    Join Date
    Apr 2004
    Posts
    204

    Re: Declaring an object using the "new" keyword

    I am trying to use GDI+ to create the metafile (file that contains all instructions to render a vector image).

  5. #5
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: Declaring an object using the "new" keyword

    Well then you have to include Gdiplus.h, use the namespace Gdiplus, link to Gdiplus.lib, initialize Gdiplus library, and finally... DECLARE myMetafile:
    Code:
       using namespace Gdiplus;
       Metafile* myMetafile = new Metafile(L"c:\\MyDiskFile.emf", hdc);
    And of course, as Paul already stated, it's good to avoid dynamic allocation with new when it's not necessary. So, better is:
    Code:
       Metafile myMetafile(L"c:\\MyDiskFile.emf", hdc);
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  6. #6
    Join Date
    Apr 2004
    Posts
    204

    Re: Declaring an object using the "new" keyword

    Thanks ovidiucucu, both of your methods work... code successfully produces a metafile.

    While we are on the subject of new, when might it be necessary to declare something this way?

    Also, I have never come across using a namespace.
    After a quick search I sort of understand what it is... why does it have to be used here?

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Declaring an object using the "new" keyword

    Quote Originally Posted by mmscg View Post
    Thanks ovidiucucu, both of your methods work... code successfully produces a metafile.
    And again, without "delete", you have a memory leak, so if you forgot to call "delete", your program really isn't working as you think it is.
    While we are on the subject of new, when might it be necessary to declare something this way?

    Also, I have never come across using a namespace.
    After a quick search I sort of understand what it is... why does it have to be used here?
    These are questions that are best answered by C++ books, as all of these issues are part of basic C++ programming. What C++ books are you using?
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
       cout << "Hello World";
    }
    This is a "Hello World" program in C++. You have never seen this before?

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; October 20th, 2010 at 09:49 AM.

  8. #8
    Join Date
    Apr 2004
    Posts
    204

    Re: Declaring an object using the "new" keyword

    Yes I used delete with the new method of declaration...
    don't know if I used it correctly, but there were no compile errors or warnings.

    Yes I've seen that Hello World program before.

    With namespaces I'd expect to see std::<some function or constant>, but I don't.

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Declaring an object using the "new" keyword

    Quote Originally Posted by mmscg View Post
    Yes I used delete with the new method of declaration...
    don't know if I used it correctly, but there were no compile errors or warnings.
    The problem is that wrong usage of delete (if you are deleting a pointer) is not flagged by the compiler. It is a runtime issue, so you only know if you did something wrong if your program doesn't behave correctly.
    Code:
    int main()
    {
       char *ptr;
       delete ptr;
    }
    This code compiles, but it is wrong. The overall message is that without learning C++ properly, you will not write programs correctly, or you may think you have a working program when it really is just working by chance or luck. C++ isn't a language where you can cherry pick what you want to use and use it properly. It doesn't work that way.

    That's why I asked what books are you using to learn C++.
    Yes I've seen that Hello World program before.

    With namespaces I'd expect to see std::<some function or constant>, but I don't.
    So I gather you aren't using C++ books. "using namespace xxx" is another way to specify that a namespace is in use. This clause is usually introduced on the first few pages of any good C++ book, so there is a big gulf somewhere in what you're learning with respect to C++.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured