我想創建一個Person對象的向量。不過,我真的想要存儲從Person基類派生的Adult,Professor和Student對象。在詢問this question之後,我瞭解到我的問題出現在Object Splicing中,並且它將派生類保存爲Person類,因爲這是該vector的內容。我試圖修改代碼來糾正這種情況,但是我仍然遇到了一些錯誤,需要一些額外的幫助。謝謝!防止對象切片 - C++
這裏是我的main.cpp代碼:
#include "Person.h"
#include "Professor.h"
#include "Student.h"
#include "Adult.h"
#include <vector>
#include <string>
#include <iostream>
using namespace std;
template<typename T> void addPerson(vector<Person *> &personVector) {
cout << "DEBUG" << endl;
personVector.push_back(new T());
}
void addFriend(vector<Person *> &personVector) {
bool loop = true;
while (loop) {
cout << "\nWhich Person would you like to create? " << endl;
cout << " 1. Professor" << endl;
cout << " 2. Student" << endl;
cout << " 3. Adult" << endl;
int caseVar;
cin >> caseVar;
switch (caseVar) {
case 1:
addPerson<Professor>(personVector);
// old incorrect line: addPerson<Professor *>(personVector);
loop = false;
break;
case 2:
addPerson<Student>(personVector);
// old incorrect line: addPerson<Student *>(personVector);
loop = false;
break;
case 3:
addPerson<Adult>(personVector);
// old incorrect line: addPerson<Adult *>(personVector);
loop = false;
break;
default: cout << "Unknown Entry Please Try Again." << endl;
}
}
}
void displayFriends(vector<Person *> &personVector) {
if (personVector.size() > 1) {
for (unsigned i = 0; personVector.size() > i; i++)
cout << i+1 << ". " << personVector[i]->getName() << endl;
cout << "DEBUG" << endl;
}
}
void viewFriend(vector<Person *> &personVector) {
if (personVector.size() > 1) {
displayFriends(personVector);
cout << "Which # friend would you like to view? ";
int num;
cin >> num;
personVector[num-1]->coutPerson();
} else if (personVector.size() == 1) {
personVector[0]->coutPerson();
} else {
cout << "No friends to View." << endl;
}
}
void deleteElementFromArray(int element, vector<Person *> &personVector) {
vector<Person *> newPersonVector;
for (unsigned i = 0; personVector.size() > i; i++)
if (i != element - 1)
newPersonVector.push_back(personVector[i]);
personVector = newPersonVector;
}
void removeFriend(vector<Person *> &personVector) {
if (personVector.size() > 1) {
displayFriends(personVector);
cout << "Which # friend would you like to remove? ";
unsigned num;
cin >> num;
if (num <= personVector.size())
deleteElementFromArray(num, personVector);
else
cout << "Invalid Selection" << endl;
} else if (personVector.size() == 1) {
cout << "Removed one and only friend" << endl;
} else {
cout << "No friends to Remove." << endl;
}
}
int main() {
vector<Person *> personVector;
// Run Main Menu
cout << "Friends with Stuff" << endl;
cout << "Adding 5 friends to the list" << endl;
personVector.push_back(new Person("James"));
personVector.push_back(new Person("Martin"));
personVector.push_back(new Person("Sammi"));
personVector.push_back(new Person("Donny"));
personVector.push_back(new Person("Ronald"));
bool loop = true;
while (loop) {
cout << "\nWhat would you like to do? " << endl;
cout << " 1. Add New Friend" << endl;
cout << " 2. View Friend" << endl;
cout << " 3. Remove Friend" << endl;
cout << " 4. Clear Friends" << endl;
cout << " 5. Get Object" << endl;
cout << " 6. Exit" << endl;
int caseVar;
cin >> caseVar;
switch (caseVar) {
case 1:
addFriend(personVector);
break;
case 2:
viewFriend(personVector);
break;
case 3:
removeFriend(personVector);
break;
case 4:
personVector.clear();
break;
case 5:
break;
case 6:
loop = false;
break;
default: cout << "Unknown Entry Please Try Again." << endl;
}
}
}
我收到以下錯誤
InitializeBuildStatus:
1> Touching "Debug\FPVisualStudio.unsuccessfulbuild".
1>ClCompile:
1> main.cpp
1>\\cs1\cs_students\mlindahl15\cs172\final project\fpvisualstudio\main.cpp(12): error C2664: 'void std::vector<_Ty>::push_back(_Ty &&)' : cannot convert parameter 1 from 'Professor **' to 'Person *&&'
1> with
1> [
1> _Ty=Person *
1> ]
1> Reason: cannot convert from 'Professor **' to 'Person *'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1> \\cs1\cs_students\mlindahl15\cs172\final project\fpvisualstudio\main.cpp(26) : see reference to function template instantiation 'void addPerson<Professor*>(std::vector<_Ty>)' being compiled
1> with
1> [
1> _Ty=Person *
1> ]
1>\\cs1\cs_students\mlindahl15\cs172\final project\fpvisualstudio\main.cpp(12): error C2664: 'void std::vector<_Ty>::push_back(_Ty &&)' : cannot convert parameter 1 from 'Student **' to 'Person *&&'
1> with
1> [
1> _Ty=Person *
1> ]
1> Reason: cannot convert from 'Student **' to 'Person *'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1> \\cs1\cs_students\mlindahl15\cs172\final project\fpvisualstudio\main.cpp(30) : see reference to function template instantiation 'void addPerson<Student*>(std::vector<_Ty>)' being compiled
1> with
1> [
1> _Ty=Person *
1> ]
1>\\cs1\cs_students\mlindahl15\cs172\final project\fpvisualstudio\main.cpp(12): error C2664: 'void std::vector<_Ty>::push_back(_Ty &&)' : cannot convert parameter 1 from 'Adult **' to 'Person *&&'
1> with
1> [
1> _Ty=Person *
1> ]
1> Reason: cannot convert from 'Adult **' to 'Person *'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1> \\cs1\cs_students\mlindahl15\cs172\final project\fpvisualstudio\main.cpp(34) : see reference to function template instantiation 'void addPerson<Adult*>(std::vector<_Ty>)' being compiled
1> with
1> [
1> _Ty=Person *
1> ]
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:02.90
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
我相信,它告訴我,我有我的代碼太多的指針。但我不明白爲什麼會這樣。我有指針的唯一地方是說矢量包含我需要在每種情況下重複。這些指針是需要的,所以我沒有對象切片。
Person.h
#ifndef PERSON_H
#define PERSON_H
#include <string>
#include "Date.h"
#include "PhoneNumber.h"
using namespace std;
class Person
{
protected:
string name;
Date birthday;
PhoneNumber phoneNumber;
string city, state;
public:
// Constructors
Person();
Person(string name) {
this->name = name;
birthday = Date();
phoneNumber = PhoneNumber("0000000000");
city = "Unknown";
state = "Unknown";
}
// Getter and Setter Methods
string getName();
void setName(string);
Date getBirthday();
// Methods
void coutPerson();
};
#endif
Person.cpp
#include "Person.h"
#include "PhoneNumber.h"
#include "Date.h"
#include <string>
#include <iostream>
using namespace std;
// Constructors
Person::Person() {
cin.ignore();
cout << "Name? ";
getline(cin, name);
cout << "Birthday: " << endl;
birthday.askUserForDate();
phoneNumber.create();
cout << "City? ";
getline(cin, city);
cout << "State? ";
getline(cin, state);
}
// Getter and Setter Methods
string Person::getName() {
return name;
}
void Person::setName(string name) {
this->name = name;
}
void Person::coutPerson() {
cout << name << endl;
birthday.coutDate();
phoneNumber.coutPhoneNumber();
cout << city << ", " << state << endl;
}
// Methods
Student.h
#ifndef STUDENT_H
#define STUDENT_H
#include "Person.h"
using namespace std;
class Student : public Person
{
private:
string dorm;
int dormRoom;
public:
// Constructors
Student();
Student(Student &);
// Getter and Setter Methods
// Methods
void coutPerson();
};
#endif
Student.cpp
#include "Student.h"
#include "Person.h"
#include <string>
#include <iostream>
using namespace std;
// Constructors
Student::Student() {
cin.ignore();
cout << "Dorm? ";
getline(cin, dorm);
cout << "Dorm Room #? ";
cin >> dormRoom;
}
// TODO Copy Constructors
// TODO Deconstructors
// Overload < > ==
Student::Student(Student &student) {
Person::Person(student);
dorm = student.dorm;
dormRoom = student.dormRoom;
}
void Student::coutPerson() {
cout << "DEBUG: Student::coutPerson()" << endl;
Person::coutPerson();
cout << "Dorm Room: " << this->dorm << " " << this->dormRoom << endl;
}
// Methods
它不告訴你,你有太多的三分球。它告訴你,你在混合不兼容的指針,在這種情況下是'T *'和'T **'。 – moshbear
你試圖實現什麼?你的addPerson將一個向量作爲參數,你想創建一個向量向量? –