Link to home
Start Free TrialLog in
Avatar of sydneyguy
sydneyguyFlag for Australia

asked on

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);

};
SOLUTION
Avatar of phoffric
phoffric

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sydneyguy

ASKER

\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;
}
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;
}
Do you still need help or is your issue resolved?
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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pepr
pepr

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.
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
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.
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
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 {}
      
};
thanks for all your help, catch you all on the next problem
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.