collapse collapse

* Who's Online

  • Dot Guests: 73
  • Dot Hidden: 0
  • Dot Users: 0

There aren't any users online.

* Board Stats

  • stats Total Members: 88
  • stats Total Posts: 11164
  • stats Total Topics: 1700
  • stats Total Categories: 4
  • stats Total Boards: 76
  • stats Most Online: 1470

Author Topic: Pointer's acting up  (Read 2766 times)

0 Members and 1 Guest are viewing this topic.

Offline spum789

  • Limited_C++_Learner
  • Jr. Member
  • *
  • Posts: 65
  • Reputation 97
  • Gender: Male
    • View Profile
Pointer's acting up
« on: February 23, 2011, 09:14:18 pm »
ok so i have declared a pointer to an object. when i declare the pointer everything is fine. The problem is when i try to use the pointer outside of the main function whether its for setting the value of one of the variables in the class(which is public). anyways here is the code

Code: C++
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. class Team {
  6. public:
  7.         int players;
  8.         string type;
  9.         string name;
  10.         Team() : players(15) {}
  11.         ~Team() {}
  12. };
  13.  
  14. Team newTeam;
  15.  
  16. Team* pp = &newTeam;
  17.  
  18. //this is the part where i get the error which is this decleration has not storage class or type specifier
  19. //also i still get the same error whether i use the "*" or not
  20. *pp->players = 7;
  21.  
  22. int main(void)
  23. {
  24.         // but when used down here it gives an error that says "operand of '*' must be a pointer
  25.          pp->players = 8;
  26.          cout << pp->players << endl;
  27.          int x = 5;
  28.          cout << x << endl;
  29.          int* px = &x;
  30.          cout << *px << endl;
  31.          *px = 7;
  32.          cout << *px << endl;
  33.          *px -= 2;
  34.          cout << *px << endl;
  35.          getchar();
  36. }

[edited name=spum789 date=1298517503][/edited]
[edited name=spum789 date=1298517620][/edited]
« Last Edit: February 23, 2011, 09:21:09 pm by spum789 »

Offline Celestialkey

  • Administrator
  • Hero Member
  • *
  • Posts: 3962
  • Reputation 4874
  • Gender: Male
  • Its Alive!!
    • View Profile
    • www.CelestialCoding.com
Re: Pointer's acting up
« Reply #1 on: February 23, 2011, 09:27:19 pm »
Code: C++
  1. #include <stdafx.h>
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. class Team {
  7. public:
  8.    int players;
  9.    string type;
  10.    string name;
  11.    Team() : players(15) {}
  12.    ~Team() {}
  13. };
  14.  
  15. Team newTeam;
  16.  
  17. Team* pp = &newTeam;
  18.  
  19. //this is the part where i get the error which is this decleration has not storage class or type specifier
  20. //also i still get the same error whether i use the "*" or not
  21. void doit(){
  22.         (*pp).players = 7;
  23. }
  24.  
  25. int main(void)
  26. {
  27.    // but when used down here it gives an error that says "operand of '*' must be a pointer
  28.         doit();
  29.     pp->players = 8;
  30.     cout << pp->players << endl;
  31.          int x = 5;
  32.     cout << x << endl;
  33.     int* px = &x;
  34.     cout << *px << endl;
  35.     *px = 7;
  36.     cout << *px << endl;
  37.     *px -= 2;
  38.     cout << *px << endl;
  39.     getchar();
  40. }

You cannot modify pointers outside of a function in the C or C++ standard that way.
Created By: Eczuo
Quote
I have noticed that people who claim that everything is predestined, and we can do nothing to change it, look both ways before they cross the road.
Quote
I'd prefer to die standing, than to live on my knees - Che Guevara
Quote
If you change the way you look at something, does that something change in any way?
- Quantum Theory

Hacking
Quote
Never in the field of human conflict was so much owed by so many to so few. - Winston Churchill


Quote from: Revelations 12:4
And his tail drew the third part of the stars of heaven, and did cast them into the earth; and the dragon stood before the woman which was ready to be delivered, for to devour her child as soon as it was born.

Quote
It takes skill to build an empire. It takes an idiot to maintain it.

 

Donate


* Search


* Recent Posts

I miss the chatbox by Shishka
[August 27, 2019, 11:52:39 pm]


Image Comparison by Shishka
[May 15, 2017, 01:18:02 pm]


Re: srchub - free source code hosting by Nathan
[December 14, 2015, 11:37:02 pm]


Re: srchub - free source code hosting by Celestialkey
[November 27, 2015, 08:51:42 am]


Updates by Nathan
[October 30, 2015, 08:27:36 pm]