954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

copy constructor & assignment operator overload problems

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
#include
#include "Person.h" // class header file
#include "building.h"

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


Person::Person(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::PersonLeavesBuilding() {
personCount--;
} [/PHP]

This is fairly new to me so any suggestions would be more than appreciated!
thanks!

mcook228
Newbie Poster
10 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

You can assign Persons, but you're trying to assign a pointer to a person.

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

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

mcook228
Newbie Poster
10 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

It should had been like:
passengerPtr[i]->personLeavesBuilding();because passengerPtr[i] will be a pointer.

officerajesh
Newbie Poster
1 post since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You