I'm somewhat new to using the 'new' operator, and I need to use it for a struct/class, but I'm not entirely sure how. This is my code, which doesn't work:
1>c:\users\-----\documents\visual studio 2010\projects\cursestest\cursestest\cursestestmain.cpp(21): error C2228: left of '.type' must have class/struct/union
1> type is 'myclass *'
1> did you intend to use '->' instead?
1>c:\users\-----\documents\visual studio 2010\projects\cursestest\cursestest\cursestestmain.cpp(22): error C2228: left of '.printexample' must have class/struct/union
1> type is 'myclass *'
1> did you intend to use '->' instead?
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I'm pretty inexperienced with this sort of coding, so I have absolutely no idea as to what to do. Any help would be appreciated.
you declared a myclass class with the same name of a myclass pointer.
What the error says is to use '->' instead of '.' for class pointers.
If you keep 'myclass p2' keep the '.'
If you keep 'myclass * p2...' use '->'
I see a memory leak at 2 sites: Lines 17 & 18. Every call to new should have a corresponding call to delete[1]. C++ doesn't have a garbage collection system, like Java, or C# (except for C++/CLI -- Microsoft's idea of garbage collection for C++ (and a terrible one at that)). You allocate memory, you deallocate it. Also, you don't seem to have grasped the concept of pointers. That being said, you shouldn't do DMA if you don't fully understand pointers.