easy c++ question re pointers

have been playing with some pointer code to jog my memory of programming procedures

https://www.youtube.com/watch?v=bxLrgCFyR4k
have been working through the above youtube tutorial

and have tbe below code set up on c++ express 2008 and have built it the same as the tutorial but when it gets to the line
leemming *leemmingObject = &leemmingObject;

it throws up the below errors, have built this several times and cannot see why it differs from the example and any help would be appreciated

1>.\main3.cpp(12) : error C3699: '*' : cannot use this indirection on type 'leemming'
1>        compiler replacing '*' with '^' to continue parsing
1>.\main3.cpp(12) : error C2373: 'leemmingObject' : redefinition; different type modifiers
1>        .\main3.cpp(9) : see declaration of 'leemmingObject'
1>.\main3.cpp(12) : error C3072: operator '&' cannot be applied to an instance of a ref class
1>        use the unary '%' operator to convert an instance of a ref class to a handle type

------------------
main 3
------------------

// main3.cpp : main project file.

#include "stdafx.h"
#include "leemming.h"
using namespace System;

int main(array<System::String ^> ^args)
{
    leemming leemmingObject;
      
      
      leemming *leemmingObject = &leemmingObject;

      leemmingObject.turtle();

      Console::WriteLine(L"Hello World");
    return 0;
}
...............................
leemming.h
..............................

#include "StdAfx.h"
#include <iostream>
#include "leemming.h"

using namespace System;


leemming::leemming(void)
{

      std::cout << " this is from the constructor" << std::endl;
}

void leemming::turtle(){
      
      std::cout << " this is from the turtle class inside the lemming class" << std::endl;
      
};

-------------------
 leemming.h
--------------------


#pragma once


ref class leemming
{
public:
      leemming(void);
      void turtle(void);

};
sydneyguyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

phoffricCommented:
>> redefinition; different type modifiers
You have leemmingObject twice:
>>   leemming leemmingObject;
>>   leemming *leemmingObject = &leemmingObject;
For the second one, you can use:
 leemming *p_leemmingObject = &leemmingObject;

Now you can say either:
    leemmingObject.turtle();
or
    p_leemmingObject->turtle();
0
sydneyguyAuthor Commented:
\main3.cpp(21) : error C3699: '*' : cannot use this indirection on type 'leemming'
so below is what i now have and what it throws up is the above now it is not using *leemmingObject but *leemmingPointer so this is equivant to your code forgettign the rest of the code still will not compile

int main(array<System::String ^> ^args)
{

            leemming leemmingObject;
              leemming *leemmingPointer = &leemmingObject;
      
      
      
      Console::WriteLine(L"Hello World");
    return 0;
}
0
sydneyguyAuthor Commented:
have gone back to just the main function and built this here to test it and it works now will rebuild the other class and place the information accross to there and try that

int main(array<System::String ^> ^args)
{
    int x = 5;
      cout << &x << endl;
      int *lemming;
      lemming = &x;
      cout << lemming << endl;
      
      
      
   // Console::WriteLine(L"Hello World");
    return 0;
}
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Karrtik IyerSoftware ArchitectCommented:
Do you still need help or is your issue resolved?
0
sydneyguyAuthor Commented:
still trying to understand the syntax of this stuff, and splitting simple functions across cloasses  and then getting the pointers to show what i want may be just keep going till it starts to make sense
0
phoffricCommented:
>> easy c++ question re pointers
I didn't realize until now that you were not writing in C++, but in a Microsoft language which is similar to C++. The syntax you show is not part of the C++ language. I am guessing you are writing in MS Managed C++ which is used in their .NET environment.

No problem for these types of questions, but in general, you will get the best type of assistance if you include Microsoft Managed C++ in your subject (or OP description).

>> still trying to understand the syntax of this stuff
I'll give you a C++ explanation, and hopefully, it will apply to MS Managed C++.

Firstly, the following program builds with no errors, and runs as expected.
#include <iostream>

class leemming
{
public:
   leemming(void)
   {
      std::cout << " this is from the constructor" << std::endl;
   }
   void turtle(void)
   {
      std::cout << " this is from the turtle **Method** inside the lemming class" << std::endl;
   }
}; 

int main()
{
   leemming leemmingObject;
   leemming *leemmingPointer = &leemmingObject;

   leemmingObject.turtle();
   leemmingPointer->turtle();

   (*leemmingPointer).turtle();

   return 0;
}

Open in new window

Line 18: leemming leemmingObject;
This is just the leeming object. That is, this takes up memory (whereas leemming is just a class that describes what goes into that memory).

Line 19: leemming *leemmingPointer = &leemmingObject;
Firstly, consider:
    leemming *leemmingPointer;
leemmingPointer is an object of type leemming *. That is, as you already seem to know from your naming convention, leemmingPointer is expected to hold a pointer (i.e., an address) of a leeming type. Currently, as shown, it is not pointing to anything. (Normally, if not pointing to anything, it is best to initialize the pointer to NULL.)

Now consider the expression:
    &leemmingObject
The & is a unary operator on the object leemmingObject. The expression returns the address of the leemmingObject.

Putting them together:
    leemming*       leemmingPointer     =     &leemmingObject;
    ---------       ---------------           ---------------
    type ptr        an object of type         address of an object
    to a leeming    ptr to a leeming.         which is of type
    class.                                    ptr to a leeming.

Open in new window

A pointer, when properly initialized has a value of the address of the object it is pointing to; and in this statement, the pointer type is set to the address of the same type, which is usually necessary to get things to compile (unless you start using casts).

Line 21: leemmingObject.turtle();
A class object can refer to its members (data or functions/methods) by using a period (.) operator.

Line 22: leemmingPointer->turtle();
An object which is a pointer to a class object can refer to its members by using a right arrow (->) operator.

Hope this brief discussion of pointers and addresses helps a bit.
Just one more thing to help avoid further possible confustion.

Line 19 is a declaration and definition of leemmingPointer. The asterisk (*) is not an operator; it is part of the data type.


Line 22: leemmingPointer->turtle();
An object which is a pointer to a class object can refer to its members by using a right arrow (->) operator.

Hope this brief discussion of pointers and addresses helps a bit.
Just one more thing to help avoid further possible confustion.

Line 19 is a declaration and definition of leemmingPointer. The asterisk (*) is not an operator; it is part of the data type. But a asterisk (*)  can also be used to dereference a pointer to yield the object that a pointer is pointing to.

In line 24, the asterisk (*) is a unary operator (called a pointer dereferencing operator) that operates on a pointer. The expression, (*leemmingPointer), returns the object that leemmingPointer is pointing  to. Since this expression is a class object, then to access the leeming class data or function members, a dot (.) operator is required, rather that the right arrow (->) operator.

Hope this helps.
1
sarabandeCommented:
your last code seem to indicate that you want to code in regular c++ rather than in managed c++. managed c++ is an own language which is able to produce intermediate code that is compatible with that of other .net languages like c# or vb.net. compared to (normal) c++ it has additional syntax elements like the '^' or '%' operator mentioned in your error message in order to handle managed (by gc = garbage collector) objects but on the other hand a lot restrictions. for example in c++ .net it is not allowed to use pointers to managed objects. because of that your current focus on pointers and pointer handling is badly supported by the managed c++.

i would recommend to create a win32 or mfc project in visual studio, change character set from 'unicode' to 'multi-byte character set' and repeat your tutorial.

Sara
1
peprCommented:
To add to the answers of the others. If you just start with C++ on Windows. I recommend to replace your Visual Studio Express 2008 by the Visual Studio 2015 Community Edition (basically free for personal usage) http://www.microsoft.com/en-us/download/details.aspx?id=48146

The reason is that it implements C++11 Standard that brings really nice features to C++ that any user/beginner wants to use -- like the new for-loop, the automatically deduced data types based on what is the type of the following expression, etc.
0
sydneyguyAuthor Commented:
first of all thank you so much for spending so much time and effort and all the comments you hav added it is most appreciated.
Yes you are right i am using visual studios 2008 express, so there are differences between the c++ languages as i am finding out, so this is one are that i am learning quickly.
I have always done bits and pieces but now have decided to get it down pat, will take some time but with nice people like your self i will get there.
so at the moment am going through multiple classes so that i can move code around and keep the program compiling seems to be the main object.
so stay tuned back soon
again thanks for your help
0
sydneyguyAuthor Commented:
thanks for all the condensed input, i did not realise there was such difference between the different versions run under the compilers, and the different versions of c++ its self, so started again from the beginning with all the data you have given me above and compile it at every change and then see what works what dosnt, and what is different, and i am getting through it, so back soon and thanks for your help.
0
sydneyguyAuthor Commented:
at this point i have the example running and am starting to understand all the differences, i was trying to do examples not realising that there are different rules to how its done , depending on what your are doing and directions you are going, is any thing easy, but trying to come up with a set of procedures to code around from conception of idea to hard code and best way to present the structure, from idea to visual on the computor. so good learning curve.
also will be going into android and sql server will also need to be attached to the system, and some web based data ins and outs as well, so lots to do. but its a start.

 i have downloaded the 2015 community have not loaded it up yet, is it worth running is it better to go with the latest or stay at 2008

is there a better compiler to use than the ms express or is it as good as any expecially if it may be linked into office down the track

after this i will end this question then start another, for on going questions
thanks for all your help so far
0
peprCommented:
If you are on Windows then VS 2015 Community Ed. is really worth of the download. The Community Edition started as VS 2013, and it is better than the Express edition. It actually has the VS 2015 Professional features with legal limitations.

I have been using the VS since version 6, mostly 2005, 2008, 2010, shortly 2012, then 2013, and now 2015. The only reason for returning back to 2005 is that we still maintain some code that was rather big and dirty -- not worth to rewrite for the newer compiler. The newer code has moved to 2008 and 2010 gradually, later to 2013 and now to 2015. The 2008 and 2010 were fine, the 2012 (fifty shades of gray) was uggly. The 2013 implemented the most of C++11, the 2015 is rather nice evolution.
0
phoffricCommented:
For Windows GUI development (which I do not do), my understanding is that the two main contenders are MFC and QT. From what I've read, MFC on new projects is used primarily when there is a need to drop down to WIndows API primitives, and also where the core team has experience with MFC.

QT has the advantage (significant, in my opinion) in that it is cross-platform allowing you to port your program much more easily to, say Linux, than with MFC.

Managed C++ is not C++. Pure ISO C++ consists of C++03, C++11, C++14, and soon to come, C++ 17.
If you write in C++03 (i.e., the formal definition of C++ that was completed in 2003), then those programs will work in the more recent C++11 and C++14 standard versions. But if you write C++11 code with its latest features, then it will not run in a C++03 compiler. (Some C++03 compilers have included some C++11 features, only to make things more confusing in terms of portability; that is, you may be thinking that you are writing C++03 code, only to find that it will not compile if you port it to another platform that supports only C++03 code.)

Although I prefer writing in pure C++ (i.e., code built using a ISO-compliant with no libraries that force you to stick with one vendor or platform), sometimes there are compelling reasons to deviate from this. For example, right now, I am using the Intel compiler, and wrote code using their IPP (Intel Performance Primitives) library. One small pure-C++ function in a program took 28 seconds to run, but with just a few code changes using IPP, the function took only 2 seconds by taking advantage of SIMD architecture.
0
peprCommented:
Just to fix some info (it is a bit off-topic ;)

Managed C++ is a Microsoft extension (including syntax) that makes the language a .NET language. One can mix the pure C++ with .NET libraries, created .NET things, etc. It is a trade-off, but some people may like it. (I have successfully avoided to use it ;)

MFC is quite old framework by Microsoft. I have successfully avoided also using that. I have used ATL (wrapper) library of the Windows API primitives (by Microsoft), and its WTL extension (by volunteers).

The Qt is a third party framework (it is not Microsoft-related) -- probably a safe bet https://en.wikipedia.org/wiki/Qt_%28software%29 
Its commercial license is probably the reason why it was accepted by many companies. (Now having also free licenses.) The dark side of the framework (in my opinion) is that it is kind of heavy weight -- many things now native to C++ are reimplemented (wrapped) by the framework.

I like more one of the competitors -- wxWidgets (https://en.wikipedia.org/wiki/WxWidgets; http://wxwidgets.org/).
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sydneyguyAuthor Commented:
thanks for your help every one, so many different syntaxs between the packages and examples,
void SetAge(int){};    is not the same as void SetAge(int);   missing the {}
throws up a
c++  error LNK2028: unresolved token (0A000290) "public: void __thiscall
error untill you put the {} at the end of the statement which is differenent to the tutorial.
seems to me you learn by debugging the exercises to get them to run and you  learn the differences as you go.
so onwards and up wards catch you soon and thanks for all your help

Dog fido;
fido.WagTail();

class Dog : public Mammal
{

public:
       // Constructors
      Dog(void){
        };
    // Other methods
      void WagTail(){}; // ******** ////////////  note note note do not forget {}
      
};
0
sydneyguyAuthor Commented:
thanks for all your help, catch you all on the next problem
0
peprCommented:
Here, the thing between the { } is the body of the function. This is what you want to implement. The void SetAge(int); is the way to tell the compiler that the function is implemented somewhere, but not here; anyway, this is how it should be called (that is the type of the returned value the name of the function, the number of arguments, the types of the arguments). This is called a function prototype.

When you use a function in your source, it must be known in advance. This is the reason for using function prototypes.

The prototypes are stored in header files (.h or .hpp extension -- just by convention).

If the function is defined (with body) earlier than it is used, then you do not need to use its prototype.

The implementation is usually stored in .c or .cpp (again by convention). Any file content can be inserted (as text for the compiler) by the #include "somefile.h" -- here for the header file that is usually included in .cpp file or in another .h file.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.