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:

#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
#include <stdlib.h> 
#include <stdio.h> 
#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--; 
}

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

Recommended Answers

All 3 Replies

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

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?

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--;
	}
}

passengerPtr has been declared as Person **passengerPtr.

Thanks...

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.