| | |
copy constructor & assignment operator overload problems
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2005
Posts: 10
Reputation:
Solved Threads: 0
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!
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!
•
•
Join Date: Jul 2005
Posts: 10
Reputation:
Solved Threads: 0
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...
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...
![]() |
Similar Threads
- please help me with my homework- priority queue (C++)
- Overloading assignment operator (C++)
- assignment operator for a 3d array (C++)
- (C++) Writing a Copy Constructor?!? (C++)
- error in copy constructor (C)
Other Threads in the C++ Forum
- Previous Thread: GUI sites
- Next Thread: STL funciton return vector of map iterators
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






