944,038 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 13378
  • C++ RSS
Jul 23rd, 2005
0

copy constructor & assignment operator overload problems

Expand Post »
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!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mcook228 is offline Offline
10 posts
since Jul 2005
Jul 23rd, 2005
0

Re: copy constructor & assignment operator overload problems

You can assign Persons, but you're trying to assign a pointer to a person.
Team Colleague
Reputation Points: 1135
Solved Threads: 172
Super Senior Demiposter
Rashakil Fol is offline Offline
2,479 posts
since Jun 2005
Jul 24th, 2005
0

Re: copy constructor & assignment operator overload problems

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...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mcook228 is offline Offline
10 posts
since Jul 2005
Apr 23rd, 2007
0

Re: copy constructor & assignment operator overload problems

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
officerajesh is offline Offline
1 posts
since Apr 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: GUI sites
Next Thread in C++ Forum Timeline: STL funciton return vector of map iterators





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC