copy constructor & assignment operator overload problems

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2005
Posts: 10
Reputation: mcook228 is an unknown quantity at this point 
Solved Threads: 0
mcook228 mcook228 is offline Offline
Newbie Poster

copy constructor & assignment operator overload problems

 
0
  #1
Jul 23rd, 2005
Would somebody mind looking at this code and giving me some advice as to the copy constructor and assignment operator overload function? When I try to use this class in my program, I get the following error:


error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class Person *' (or there is no acceptable conversion)

header file:

[PHP]#ifndef Person_H
#define Person_H

// Class Name: Person
// Class Language: C++
// Class Filename: Person.h

class Person {

// Attributes:

private:
// private data members:

int ID;
int personCount;
int weight;
int waitTime;

// Operations:

public:
// public member functions:

Person();
~Person();
Person(const Person& aPerson);
Person &operator=(const Person &right);
void PersonLeavesBuilding();

};
#endif // Person_H [/PHP]

[PHP]#include <stdlib.h>
#include <stdio.h>
#include "Person.h" // class header file
#include "building.h"

Person:erson()
{
personCount++;
ID=personCount;
waitTime=(1+rand()%100);
}


Person:erson(const Person& aPerson) {
ID=aPerson.ID;
personCount=aPerson.personCount;
waitTime=aPerson.waitTime;
weight=aPerson.weight;
}

Person &Person::operator=(const Person &right) {
if (&right != this) {
ID=right.ID;
personCount=right.personCount;
waitTime=right.waitTime;
weight=right.weight;
}
return (*this);
}

void Person:ersonLeavesBuilding() {
personCount--;
} [/PHP]

This is fairly new to me so any suggestions would be more than appreciated!
thanks!
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,039
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: copy constructor & assignment operator overload problems

 
0
  #2
Jul 23rd, 2005
You can assign Persons, but you're trying to assign a pointer to a person.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 10
Reputation: mcook228 is an unknown quantity at this point 
Solved Threads: 0
mcook228 mcook228 is offline Offline
Newbie Poster

Re: copy constructor & assignment operator overload problems

 
0
  #3
Jul 24th, 2005
Given the class above (I did get the assignment overload to work), why would I be getting this error:
error C2228: left of '.personLeavesBuilding' must have class/struct/union type

on this piece of code?

[PHP]
void Elevator::passengerExits(Floor* bFloors) {
if(floorNumRef==0) {
for(int i=0; i<passCount; i++) {
passengerPtr[i].personLeavesBuilding();
}
}
else {
bFloors[floorNumRef].acceptPerson(passengerPtr[passCount]);
passCount--;
}
}

[/PHP]

passengerPtr has been declared as Person **passengerPtr.

Thanks...
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 1
Reputation: officerajesh is an unknown quantity at this point 
Solved Threads: 0
officerajesh officerajesh is offline Offline
Newbie Poster

Re: copy constructor & assignment operator overload problems

 
0
  #4
Apr 23rd, 2007
It should had been like:
passengerPtr
[i]->personLeavesBuilding();because passengerPtr[i] will be a pointer.
Last edited by officerajesh; Apr 23rd, 2007 at 8:38 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC