This is what I have so far...I can\'t get it to compile. *********Person.hpp****
ID: 3600999 • Letter: T
Question
This is what I have so far...I can't get it to compile.
*********Person.hpp**********
#ifndef PERSON_HPP
#define PERSON_HPP
#include <string>
#include <vector>
using namespace std;
class Person
{
private:
std::string name;
double age;
public:
const std::string getName();
double getAge();
Person(std::string, double);
Person() { ; }
};
#endif
*********Person.cpp**********
#include<iostream>
#include<string>
#include <vector>
#include "Person.hpp"
using namespace std;
Person::Person(std::string personName, double personAge)
{
name = personName;
age = personAge;
}
const std::string Person::getName()
{
return name;
}
double Person::getAge()
{
return age;
}
*********personSearch.cpp**********
#include<string>
#include <vector>
#include "Person.hpp"
using namespace std;
int personSearch(const vector<Person>array, string value)
{
int first = 0, //First array element
last = array.size() -1, // Last array element
middle, //Midpoint of search
position = -1; //Position of search
bool found = false; //Flag
while (!found && first <= last)
{
middle = (first + last) / 2; //Calculate midpoint
if (value.compare(array[middle].getName()) == 0) // If value is found at mid
{
found = true;
position = middle;
}
else if (value.compare(array[middle].getName()) > 0) //If person is in lower half
last = middle - 1;
else
first = middle + 1; //If person is in upper half
}
return position;
}
********Main.cpp**************
#include<iostream>
#include<string>
#include <vector>
#include "Person.hpp"
using namespace std;
int personSearch(const vector<Person>array, string value);
int main()
{
Person p0("Andre", 42);
Person p1("Bartholomew", 19);
Person p2("Connor", 54);
std::vector<Person>array;
array.push_back(p0);
array.push_back(p1);
array.push_back(p2);
int index = personSearch(array, "Andre");
system("pause");
return 0;
}
Again, I can't get my code to compile. If you could fix my code and point out where my errors are with comments, that would help.
Modify the binary search function presented in chapter 9 so that it takes two parameters: a const vector of Persons (which will already be sorted by name), and the name of the Person being searched for (use the Person class from Project 4.b). Because a vector knows its own length, no size parameter is needed. If the Person object with the matching name is found in the vector, the index of its position should be returned. Otherwise it should return -1. Because the vector of Persons is being passed by const, you'll need to make the getName0 method a constant member function, to reassure the compiler that the getName) method isn't changing any Person objects. The files must be named Person.hpp, Person.cpp and personSearch.cppExplanation / Answer
Hi To make it compile you have to fix code in Person.cpp and Person.hpp if you have not const compiler will throw errors
#include<iostream>
#include<string>
#include <vector>
#include "Person.hpp"
using namespace std;
Person::Person(std::string personName, double personAge)
{
name = personName;
age = personAge;
}
const std::string Person::getName() const {
return name;
}
double Person::getAge() const
{
return age;
}
=========
#ifndef PERSON_HPP
#define PERSON_HPP
#include <string>
#include <vector>
using namespace std;
class Person
{
private:
std::string name;
double age;
public:
const std::string getName() const ;
double getAge() const ;
Person(std::string, double) ;
Person() {
;
}
};
#endif
===============
Please correct your alogitham accordingly
====
Please let me know if you need more information.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.