Jump to content
  • Advertisement
Sign in to follow this  
mattchew008

*Cannot use public class functions

This topic is 4972 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, some odd reason I cannot use a public class function from a base class. I'm not sure what the reason for this is, but this is the error I get. ------ Compiling... Assignment2test.cpp D:\Documents and Settings\Mattchew008\My Documents\SCHOOL\1-2004 FALL\test\Assignment2test.cpp(267) : error C2228: left of '.move' must have class/struct/union type Error executing cl.exe. Assignment2test.obj - 1 error(s), 0 warning(s) ------
#include<stdio.h>
#include<stdlib.h>
#include<string>
#include <iostream>
using namespace std;


class Junkyard  
{
private:
	int NumBusDoors; 
	int NumTruckDoors; 
	int NumCarDoors; 
	int NumWheels; 

public:
	Junkyard () {NumBusDoors = 0; NumTruckDoors = 0; NumCarDoors; NumWheels;}

void Junkyard::AddBusDoors(int numDoors) {
	NumBusDoors = NumBusDoors + numDoors;
}

void JunkyardAddTruckDoors(int numDoors) {
	NumTruckDoors = NumTruckDoors + numDoors;
}

void JunkyardAddCarDoors(int numDoors) {
	NumCarDoors = NumCarDoors + numDoors;
}

void JunkyardAddWheels(int numWheels) {
	NumWheels = NumWheels + numWheels;
}

void JunkyardAdvertise() { //print the content of the junkyard to the screen in order to show the stocks this junkyard has
	cout << "Bus Doors: " << NumBusDoors << "\nTruck Doors: " << NumTruckDoors << "\nCar Doors: " << NumCarDoors << "\nWheels: " << NumWheels;
}

virtual ~Junkyard();

};


class Location  
{
public:
	int x,y;
	Location() {
		x = 0;
		y = 0;
	}
	virtual ~Location() {
		delete this;
	}


	Location(int a,int b) { 
		x = a; 
		y = b;
	}

	void Location::setLocation (int a, int b) {
		x = a;
		y = b;
	}

};

class Road 
{
public:
	string RoadName; 
	Location EntryEndPoint; 
	Location ExitEndPoint; 
	enum Direction {East, South} Dir ;

	Road(); 

	Road(string Rname, Location EntryEP, Location ExitEP, Direction aDir)
	{
		RoadName = Rname;
		EntryEndPoint = EntryEP;
		ExitEndPoint = ExitEP;
		Direction Dir= aDir;
	};

	virtual ~Road();

	void setDirEast() {
		Dir = East;
	}
	void setDirSouth() {
		Dir = South;
	}
};

class Vehicle {
public:
	string Name ;
	int Speed ;
	int LoopsPerM ;
	int CurLoopCount ;
	int NumWheels ;
	int NumDoors ;
	Location Position ;
	Road* RoadPtr ;
	
	Vehicle () {
		Speed = 0;
		LoopsPerM = 0;
		CurLoopCount = 0;
		NumWheels = 0;
		NumDoors = 0;
		RoadPtr = '\0';
	}; //initializes all attributes to zero

	Vehicle (string name, int speed, Road* rdPtr) { 
		LoopsPerM = (int) (3.6/Speed*100); 
		CurLoopCount = 0;
	}

	virtual ~Vehicle() {
		cout << "vehicle " << Name << " is destroyed";
	};
	virtual JunkIt(Junkyard &jy); 
	
	void move (Road* R1, Road* R2) { 
               CurLoopCount++;

               if (CurLoopCount == LoopsPerM) {

                 if (RoadPtr->Dir == 0) { 

                    Position.x++;

                    if (Position.x == RoadPtr->ExitEndPoint.x) {

                       RoadPtr = R2;

                       Position.x = 500;

                       Position.y = 1000;
                    }
                  }

                 else { // South

                    Position.y--; 

                    if (Position.y == RoadPtr->ExitEndPoint.y) {

                       RoadPtr = R1;

                       Position.x = 0;

                       Position.y = 500;

                    }
                 }
			  }
	}
};

class Car: public Vehicle{
public:
	bool MoonRoof;
	Car (string name, int speed, bool MoonRoof, Road* rdPtr ) {
		Name = name;
		Speed = speed;
		MoonRoof = MoonRoof;
		RoadPtr = rdPtr;
		NumWheels = 4;
		NumDoors = 4;
	}
	Car (string name, int speed, Road* rdPtr) {
		Name = name;
		Speed = speed;
		RoadPtr = rdPtr;
		NumWheels = 4;
		NumDoors = 4;
	}
	void JunkIt(Junkyard &jy, int);//update jy’s NumCarDoors and NumWheels using the correspongin public mehtod } 
	~Car() { cout << "Car " << Name << " is destroyed";} ;
};
void Car::JunkIt (Junkyard &jy, int x = 0) {
	jy.JunkyardAddCarDoors(NumDoors);
	jy.JunkyardAddWheels(NumWheels);
};

class Bus: public Vehicle{
public:
	enum Style {Single, DoubleStories, Attached} BusType ;
	Bus (string name, int speed, enum Style bType, Road* rdPtr) {
		Name = name;
		Speed = speed;
		BusType = bType;
		RoadPtr = rdPtr;
		NumWheels = 6;
		NumDoors = 3;
	}
	Bus (string name, int speed, Road* rdPtr) {
		Name = name;
		Speed = speed;
		RoadPtr = rdPtr;
		NumWheels = 6;
		NumDoors = 3;
	}
	void JunkIt(Junkyard &jy, int); //update jy’s NumBusDoors and NumWheels using the correspongin public mehtod } 
	~Bus() { cout << "Bus " << Name << " is destroyed";} ;
};
void Bus::JunkIt (Junkyard &jy, int x = 0) {
	jy.AddBusDoors(NumDoors);
	jy.JunkyardAddWheels(NumWheels);
};

class Truck: public Vehicle {
public:
	Truck(string name, int speed, Road* rdPtr) : Vehicle(name, speed, rdPtr) {NumWheels = 6; NumDoors = 2;} ;
	void JunkIt(Junkyard &jy, int);//update jy’s NumTruckDoors and NumWheels using the correspongin public mehtod} 
	~Truck() { cout << "Truck " << Name << " is destroyed";} ;
};
void Truck::JunkIt (Junkyard &jy, int x = 0) {
	jy.JunkyardAddTruckDoors(NumDoors);
	jy.JunkyardAddWheels(NumWheels);
};

class Motorcycle: public Vehicle {
public:
	Motorcycle (string name, int speed, Road* rdPtr) : Vehicle(name, speed, rdPtr) {} ;
	void JunkIt(Junkyard &jy, int){}; //nothing to be junked } 
	~Motorcycle() { cout << "Motorcycle " << Name << " is destroyed";} ;
};


int main()
{
	Location Epr11(0,500);
	Location Epr12(1000,500);
	Location Epr21(500,1000);
	Location Epr22(500,0);
	
	Road x,y;
	x.setDirEast();
	Road Road1("Road1", Epr11, Epr12, x.Dir);

	y.setDirSouth();
	Road Road2("Road2", Epr21, Epr22, y.Dir);

	Junkyard MyJunkyard;
	
	Vehicle* Vehicle[12];
	Vehicle[0] = new Car("Car1", 30, &Road1);
	Vehicle[1] = new Car("Car2", 120, &Road1);
	Vehicle[2] = new Truck("Truck1", 51, &Road1);
	Vehicle[3] = new Bus("Bus1", 90, &Road1);
	Vehicle[4] = new Bus("Bus2", 45, &Road1);
	Vehicle[5] = new Motorcycle("Motor1", 40, &Road1);
	Vehicle[6] = new Car("Car3", 180, &Road2);
	Vehicle[7] = new Truck("Truck2", 71, &Road2);
	Vehicle[8] = new Truck("Truck3", 60, &Road2);
	Vehicle[9] = new Bus("Bus3", 36, &Road2);
	Vehicle[10] = new Motorcycle("Motor2", 10, &Road2);
	Vehicle[11] = new Motorcycle("Motor3", 5, &Road2);

	for (int loop = 0; loop < 1000000; loop++) {
		for (int v = 0; v < 12; v++) {
			Vehicle[v].move(Road1,Road2);   //****LINE 267*****
		}
	}



	return 0;
};


Thank you advance for the help, I'm sorry I ask so many questions... Matt

Share this post


Link to post
Share on other sites
Advertisement
Quickly browsed through, didn't read everything,

but you might want to try '->' instead of '.'
because you are referencing on object over a pointer

Use -> for pointer,
a dot if it's not.
? : is confusing,
use them a lot.

Share this post


Link to post
Share on other sites
Vehicle[v] is a pointer, not an object. You need to dereference (get at the object it points to) the pointer before you can use it. You can do this either by explicitly dereferencing: (*Vehicle[v]).move(Road1, Road2); or by using the pointer-to-member operator: Vehicle[v]->move(Road1, Road2);.

Enigma

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!