New to Pointers -- Need help with some Code!

Hi everyone,

Longtime java programmer here, getting my feet wet in C++ on a summer project. Most of the syntax and stuff is easy to overcome, but I've been struggling a bit with pointers and their syntax.

For example, in the following test code I made, the Driver class accepts two pointers in the constructor, which then are carried along by two other class-specific pointers. However, when I compile the code, I get the following message:

1> error C2227: left of '->resetSpeed' must point to class/struct/union/generic type
1>          type is 'Auto **'
1>: error C2227: left of '->useTheTimer' must point to class/struct/union/generic type
1>          type is 'TimeKeeper **'
1>: error C2227: left of '->runTheTimer' must point to class/struct/union/generic type
1>          type is 'TimeKeeper **'
1>error C2227: left of '->checkCurrentSpeed' must point to class/struct/union/generic type
1>          type is 'Auto **'
1>c: fatal error C1903: unable to recover from previous error(s); stopping compilation


I realize the main method below doesn't do much of anything, but I want to solve the above error before proceeding any further. I've copied the code below, but I've left out many of the other classes which compile fine.

What am I doing wrong? Thanks in advance!

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
#include "Auto.h"
#include "McLaren.h"
#include "TimeKeeper.h"
#pragma once

class Driver
{
public:
	Driver(Auto *car, TimeKeeper *c);
	virtual ~Driver();

	Auto **pcar;
	TimeKeeper **clock;

	void beginRace()
	{
		pcar->resetSpeed();		
		clock->useTheTimer(true);
		clock->runTheTimer();
	}

	double calculateCurrentSpeed()
	{
		double speedx;
		double speedy;
		double accelerationx;
		double timex;

		while (pcar->checkCurrentSpeed())
		{
			timex = currenttime;
			currenttime = clock->getTheTimer();

			speedx = pcar->getCurrentSpeed();
			accelerationx = pcar->getAcceleration();

			speedy = (accelerationx * (currenttime - timex)) + speedx;
			return speedy;
		}
	}

	void launchDestroyer()
	{
		delete pcar;
		delete clock;
	}

private:
	double currenttime;

};

int main()
{
	McLaren cool;
	TimeKeeper time;

	Auto *sportscar = new McLaren();
	TimeKeeper *clock = new TimeKeeper();

	*sportscar = cool;
	*clock = time;

	Driver *drive = new Driver(*sportscar, *clock);

	return 0;
}


1
2
3
4
5
6
7
8
9
10
11
#include "Driver.h"

Driver::Driver(Auto *car, TimeKeeper *c)
{
	pcar =  car;
	clock =  c;
}

Driver::~Driver()
{
}
Last edited on
I wonder are you realy a Longtime java programmer ?! It is funny!
I thought that every programmer can see the difference between

Auto *car and Auto **pcar

and will not do the assignment pcar = car; as you do in the code.

1
2
3
4
5
Driver::Driver(Auto *car, TimeKeeper *c)
{
	pcar =  car;
	clock =  c;
}

Last edited on
The book I'm using mentions a "double pointer" that you need to use whenever you want to point to a pointer.

That's why I am using the double pointer.
In this case you should explicitly specify reference operator as

pcar = &car;

But in this case all other your code will be incorrect as for example this

1
2
3
4
5
6
	void beginRace()
	{
		pcar->resetSpeed();		
		clock->useTheTimer(true);
		clock->runTheTimer();
	}


The whole function is invalid due to invalid constructions as pcar->resetSpeed();

I think you should start from reading some book on C++ for beginners or on Java for beginners.
I got it working.

I don't need to use pcar = &car, or the double pointers.

I just had to remove some of the middlemen pointers and it seemed to straighten itself out. All functions work as intended, and outputs look good so far.

Thanks for your help, I guess!
Last edited on
Topic archived. No new replies allowed.