OOP Classes And Pointers.

I really struggle with grasping the concept of pointers. I am trying to compile this code...
//Implementation file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
 #include "Card.h"
Card:: Card(string num)
{
  cardNumber = num;
  
  
}
		
		
Card:: ~Card()
{
  
}
		
		
bool Card:: checkPin(int input) const
{
  if (inout == pin)
    return true;
  else 
    return false;
}
		
	
bool Card:: withdraw(float amount)
{
  float gb;
  gb = attachedAccount.getBalance();
  
  if (amount == gb)
  {
    attachedAccount.decreaseBalance(amount)
    return true;
  }
  else 
    return false;
}
		
	
float Card:: checkBalance() const
{
  
}
		
	
string Card:: getAccountHolder() const
{
 return  attachedAccount.getAccountHolder();
}
		
string Card:: getCardNumber() const
{
  return  attachedAccount.getAccountNumber();
}
		
		
void Card:: activateCard(Account* acc, int p)
{
  attachedAccount = acc;
  pin = p;
}
		
	
void Card:: deactivateCard()
{
  delete attachedAccount;
  pin = -999
}
		
		
void Card:: deposit(float amount)
{
  attachedAccount.increaseBalance(amount);
}
		
void Card:: setCardNumber(string num)
{
  cardNumber = num;
}


//Header File..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#ifndef CARD_H
#define CARD_H

#include "Account.h"
#include <string>

using namespace std;

class Card
{
	protected:
		
		Account* attachedAccount;
		
	
		string cardNumber;
	
	private:
		
		int pin;
	
	public:
		
		Card(string num);
		
		
		~Card();
		
		
		bool checkPin(int input) const;
		
		
		bool withdraw(float amount);
		
	
		float checkBalance() const;
		
	
		string getAccountHolder() const;
		
		string getCardNumber() const;
	
		void activateCard(Account* acc, int p);
		
		
		void deactivateCard();
		
	
		void deposit(float amount);
		
		void setCardNumber(string num);
};

#endif 


HOwever I recieve these errors..

Card.cpp: In member function ‘bool Card::withdraw(float)’:
Card.cpp:37:24: error: request for member ‘getBalance’ in ‘((Card*)this)->Card::attachedAccount’, which is of pointer type ‘Account*’ (maybe you meant to use ‘->’ ?)
Card.cpp:41:21: error: request for member ‘decreaseBalance’ in ‘((Card*)this)->Card::attachedAccount’, which is of pointer type ‘Account*’ (maybe you meant to use ‘->’ ?)
Card.cpp:42:5: error: expected ‘;’ before ‘return’
Card.cpp: In member function ‘std::string Card::getAccountHolder() const’:
Card.cpp:63:26: error: request for member ‘getAccountHolder’ in ‘((const Card*)this)->Card::attachedAccount’, which is of pointer type ‘Account* const’ (maybe you meant to use ‘->’ ?)
Card.cpp: In member function ‘std::string Card::getCardNumber() const’:
Card.cpp:68:27: error: request for member ‘getAccountNumber’ in ‘((const Card*)this)->Card::attachedAccount’, which is of pointer type ‘Account* const’ (maybe you meant to use ‘->’ ?)
Card.cpp: In member function ‘void Card::deactivateCard()’:
Card.cpp:85:1: error: expected ‘;’ before ‘}’ token
Card.cpp: In member function ‘void Card::deposit(float)’:
Card.cpp:93:19: error: request for member ‘increaseBalance’ in ‘((Card*)this)->Card::attachedAccount’, which is of pointer type ‘Account*’ (maybe you meant to use ‘->’ ?)


Please Help...It will really be appreciated

Last edited on
You declared data member attachedAccount as a pointer

Account* attachedAccount;

By the way never use such a syntax as

Account* attachedAccount;

because it only confuses readers and contradicts the C++ grammar. Do not repeat this stupidity after some idiots.
Instead write

Account *attachedAccount;

As attachedAccount is a pointer then the correct syntax to access functions or objects using pointers is

gb = attachedAccount->getBalance();
Last edited on
And Then I do that for everything account is pointing to? And instead of the dot operator I have to use the arrow operator?
Last edited on
vlad wrote:

By the way never use such a syntax as

Account* attachedAccount;


I prefer that syntax because the * is part of the type, so keeping it with the type makes more sense.

I don't consider myself an idiot.

I think you should be less aggressive when stating your opinions.
Last edited on
closed account (z05DSL3A)
I prefer Account * attachedAccount;, makes more sense to me (especially when you start bringing const in to it)
@Disch
I prefer that syntax because the * is part of the type, so keeping it with the type makes more sense.


It has no sense simply because it contradicts the C/C++ grammar. Moreover it has different meaning in C#. If your aim is to confuse readers then you selected the right way.
Last edited on
It has no sense simply because it contradicts the C/C++ grammar.

No, it doesn't.

Moreover it has different meaning in C#.

So what?

If your aim is to confuse readers then you selected the right way.

I've never met anyone who was confused by it outside of multiple pointer/non-pointer declarations on one line (regardless of the style used to declare individual variables.) If we abstain from doing that as a practice, where is the cause for confusion (aside from people who can't remember which language they're using?)
@vlad:

You're certainly entitled to your opinion. I'm not going to start a debate as to what coding style is superior because that's a debate nobody can ever win.

My point was that you can express your viewpoint without being insulting to people who have opinions which differ from yours. Calling someone 'idiots' or referring to their practices as 'stupidity' is abrasive. Especially in a beginners forum.
@cire


It has no sense simply because it contradicts the C/C++ grammar.

No, it doesn't.


You are wrong. There is no such a type specifier as T* in C/C++. Declarators and type specifiers are different things. Read the C++ Standard what are declarators and what are type specifiers.
In declaration

T* t1, t2;

T* is not a type specifier and t1 is not a declarator. So as I said if your aim is to confuse readers you seleted the write way.
Last edited on
closed account (zb0S216C)
@Vald: 9/10 C++ programmers can easily figure out that any of the following are pointers declarations:

1
2
3
4
int *A;
int* B;
int * C;
int                                           *                 D; 

As Disch said, it's a matter of preference. Also, the difference between declarators and type-specifiers really doesn't matter here unless you're writing a parser/lexer. I bet that most beginners don't know what declarators and type-specifiers are, but still would able to point out a pointer declaration -- irrespective of what the declaration looks like.

On the flip-side, introducing unnecessary terminology into an argument will only confuse the matter, not make more sense of it.

Wazzak
Last edited on
Thats the beauty of programming, there are many angles to one point. Thank you Vlad I got it working! And thank you to the rest was interesting and kind of helped me with pointer understanding.
closed account (z05DSL3A)
Considering that the standard is strewn with examples of the form T* D it kind of think meh!
vlad wrote:
I already wrote very clear for the author of the topic "Do not repeat this stupidity after some idiots."


I'd rather be an idiot than an asshole.

You're a smart guy and a good programmer, vlad, but it's really hard to read your posts a lot of times because of the way you come across.

Maybe you should work on your people skills.
Last edited on
Vlad, what's up man? You're usually pretty mild mannered.

Anyways, I use type* name;

It only gets confusing when you mix pointer and non-pointer declarations in one line. But the easy (and sensible) solution to that is just don't mix.
You are wrong. There is no such a type specifier as T* in C/C++. Declarators and type specifiers are different things. Read the C++ Standard what are declarators and what are type specifiers.

I'm aware of that. However it doesn't follow that the style contradicts the grammar. What the standard says is:

The three components of a simple-declaration are the attributes (7.6), the specifiers (decl-specifier-seq; 7.1)
and the declarators (init-declarator-list). The specifiers indicate the type, storage class or other properties
of the entities being declared. The declarators specify the names of these entities and (optionally) modify
the type of the specifiers with operators such as * (pointer to) and () (function returning).


And it certainly makes sense to me to prefer to put the operator next to what it is modifying. If anyone wants to call me an idiot for that, have at it.
Is "int* p;" right or is "int *p;" right?

Both are "right" in the sense that both are valid C and C++ and both have exactly the same meaning. As far as the language definitions and the compilers are concerned we could just as well say "int*p;" or "int * p;"
The choice between "int* p;" and "int *p;" is not about right and wrong, but about style and emphasis. C emphasized expressions; declarations were often considered little more than a necessary evil. C++, on the other hand, has a heavy emphasis on types.

A "typical C programmer" writes "int *p;" and explains it "*p is what is the int" emphasizing syntax, and may point to the C (and C++) declaration grammar to argue for the correctness of the style. Indeed, the * binds to the name p in the grammar.

A "typical C++ programmer" writes "int* p;" and explains it "p is a pointer to an int" emphasizing type. Indeed the type of p is int*. I clearly prefer that emphasis and see it as important for using the more advanced parts of C++ well.

The critical confusion comes (only) when people try to declare several pointers with a single declaration:

int* p, p1; // probable error: p1 is not an int*

Placing the * closer to the name does not make this kind of error significantly less likely.
int *p, p1; // probable error?

Declaring one name per declaration minimizes the problem - in particular when we initialize the variables. People are far less likely to write:
int* p = &i;
int p1 = p; // error: int initialized by int*
And if they do, the compiler will complain.

Whenever something can be done in two ways, someone will be confused. Whenever something is a matter of taste, discussions can drag on forever. Stick to one pointer per declaration and always initialize variables and the source of confusion disappears.

http://www.stroustrup.com/bs_faq2.html#whitespace

Incidently, precisely the same declaration grammar syntax is used to bind the & while declaring references.

Though I haven't too many debates on whether
template < typename T > void foo( const T& r ) is "right", or
template < typename T > void foo( const T &r ) is "right".

Probably because the "typical C programmer" doesn't have much to say on the subject.
Last edited on
Vlad, what's up man? You're usually pretty mild mannered.

No, he's not. Insulting people who disagree with him, and even insulting beginners who don't understand his posts, is pretty common behaviour for Vlad. He's been reported many times for it, but I guess the forum admins aren't interested.
Last edited on
No, he's not. Insulting people who disagree with him, and even insulting beginners who don't understand his posts, is pretty common behaviour for Vlad

Ah well I must have missed all this. That's unfortunate
Topic archived. No new replies allowed.