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?
Re: Declaring an object using the "new" keyword
Quote:
Originally Posted by
mmscg
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
Re: Declaring an object using the "new" keyword
Metafile sounds like a GDI+ class.
Isn't it?
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).
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);
Re: Declaring an object using the "new" keyword
Thanks ovidiucucu, both of your methods work... code successfully produces a metafile. :D
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?
Re: Declaring an object using the "new" keyword
Quote:
Originally Posted by
mmscg
Thanks ovidiucucu, both of your methods work... code successfully produces a metafile. :D
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.
Quote:
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
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.
Re: Declaring an object using the "new" keyword
Quote:
Originally Posted by
mmscg
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++.
Quote:
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