In: Computer Science
Constructors/Destructors - Initialize your data. Allocate memory if using a dynamically allocated array. The destructor should deallocate the memory. The constructor should take a single int variable to determine the size. If no size is specified (default constructor), then the size should be set to 50.
operator[](size_t) - This should return the location with the matching index. For example if given an index of 3, you should return the location at index 3 in the list.
Location class/struct - This class/struct should have the following public member variables: name, address, city, postal code, province, latitude, longitude, minimum price range, and max price range.
#pragma once
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
// "Location.h"
using std::getline;
using std::ifstream;
using std::string;
using std::stringstream;
class Location {
public:
string name, address, city, postalCode, province;
double latitude, longitude;
int priceRangeMax, priceRangeMin;
};
class PizzaZine {
private:
Location *pizzaLocations;
size_t size;
public:
// PizzaZine(const size_t & = 50);
// work
PizzaZine() { pizzaLocations = new Location[50]; }
~PizzaZine() { delete[] pizzaLocations; }
Location &operator[](size_t);
// This function is implemented for you
void readInFile(const string &);
};
// PizzaZine::PizzaZine() { pizzaLocations = new Location[50];
}
// PizzaZine::~PizzaZine() { delete[] pizzaLocations;
}
Location &PizzaZine::operator[](size_t index) { return pizzaLocations[index]; } // this is where the code ends of my written code rest is skeleton
void PizzaZine::readInFile(const string &filename)
{
ifstream inFile(filename);
Location newLoc;
string line;
string cell;
// Read each line
for (int i = 0; i < size; ++i) {
// Break each line up into 'cells'
getline(inFile, line);
stringstream lineStream(line);
while (getline(lineStream, newLoc.name, ',')) {
getline(lineStream, newLoc.address, ',');
getline(lineStream, newLoc.city, ',');
getline(lineStream, cell, ',');
if (!cell.empty()) {
newLoc.postalCode = stoul(cell);
}
getline(lineStream, newLoc.province, ',');
getline(lineStream, cell, ',');
newLoc.latitude = stod(cell);
getline(lineStream, cell, ',');
newLoc.longitude = stod(cell);
newLoc.priceRangeMin = -1;
getline(lineStream, cell, ',');
if (!cell.empty()) {
newLoc.priceRangeMin = stoul(cell);
}
newLoc.priceRangeMax = -1;
getline(lineStream, cell, ',');
if (!cell.empty() && cell != "\r") {
newLoc.priceRangeMax = stoul(cell);
}
pizzaLocations[i] = newLoc;
}
}
}
the main cpp shouldnt matter as much as all of that was skeleton code
main.cpp:15:15: error: no matching constructor for
initialization of 'PizzaZine'
PizzaZine top10(10);
^ ~~
./PizzaZine.h:20:7: note: candidate constructor (the implicit copy
constructor) not viable: no known conversion from
'int' to 'const PizzaZine' for 1st argument
class PizzaZine {
^
./PizzaZine.h:28:3: note: candidate constructor not viable:
requires 0 arguments, but 1 was provided
PizzaZine() { pizzaLocations = new Location[50]; }
^
main.cpp:29:15: error: no matching constructor for initialization
of 'PizzaZine'
PizzaZine top200(200);
^ ~~~
./PizzaZine.h:20:7: note: candidate constructor (the implicit copy
constructor) not viable: no known conversion from
'int' to 'const PizzaZine' for 1st argument
class PizzaZine {
^
./PizzaZine.h:28:3: note: candidate constructor not viable:
requires 0 arguments, but 1 was provided
PizzaZine() { pizzaLocations = new Location[50]; }
^
main.cpp:36:15: error: no matching constructor for initialization
of 'PizzaZine'
PizzaZine top400(400);
^ ~~~
./PizzaZine.h:20:7: note: candidate constructor (the implicit copy
constructor) not viable: no known conversion from
'int' to 'const PizzaZine' for 1st argument
class PizzaZine {
^
./PizzaZine.h:28:3: note: candidate constructor not viable:
requires 0 arguments, but 1 was provided
PizzaZine() { pizzaLocations = new Location[50]; }
^
3 errors generated.
class PizzaZine { private: Location *pizzaLocations; size_t size; public: // PizzaZine(const size_t & = 50); // work PizzaZine() { pizzaLocations = new Location[50]; this->size = 50; } PizzaZine(size_t size) { pizzaLocations = new Location[size]; this->size = size; } ~PizzaZine() { delete[] pizzaLocations; } Location &operator[](size_t); // This function is implemented for you void readInFile(const string &); };
************************************************** You should just add the parameterized constructor and most of errors will be going away. Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.