Question

In: Computer Science

Constructors/Destructors - Initialize your data. Allocate memory if using a dynamically allocated array. The destructor should...

  • 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.

Solutions

Expert Solution

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.


Related Solutions

Create C program that takes 1 parameter: a number. Using that number, dynamically allocate a memory...
Create C program that takes 1 parameter: a number. Using that number, dynamically allocate a memory so you store that number of integers. Write integers in order starting from 1 until you fill all that memory. Print the addresses and values of the first and the last integer stored in the memory.
Create C program that takes 1 parameter: a number. Using that number, dynamically allocate a memory...
Create C program that takes 1 parameter: a number. Using that number, dynamically allocate a memory so you store that number of integers. Write integers in order starting from 1 until you fill all that memory. Print the addresses and values of the first and the last integer stored in the memory. Help ASAP!!!
Write a modified version of the program below. In this version, you will dynamically allocate memory...
Write a modified version of the program below. In this version, you will dynamically allocate memory for the new C-string and old C-string, using the new keyword. Your program should ask the user for the size of the C-string to be entered, and ask the user for the C-string, then use new to create the two pointers (C-strings).   Then, call Reverse, as before. Don’t forget to use delete at the end of your program to free the memory! #include <iostream>...
4.2 Lab: new and delete new is the operator used to dynamically allocate memory while the...
4.2 Lab: new and delete new is the operator used to dynamically allocate memory while the program is running delete is the operator used to release memory when it is no longer needed, so it could be used next time new is used This lab gives you an example of using new and delete with an array of structures. Your tasks are listed below: review the existing code finish writing the copyList()function // Lab: new and delete #include <iostream> #include...
Prime Sum C program !! Dynamically allocated memory Let P(n) denote the sum of the first...
Prime Sum C program !! Dynamically allocated memory Let P(n) denote the sum of the first n prime numbers. For example, P(1) = 2 and P(3) = 10, since the first three prime numbers are 2, 3 and 5, respectively. Write a program to determine the value of the function P(n) for different values of n. The first few prime sums are 2, 5, 10, 17, 28, 41, 58 and 77. Input The first line of the input file contains...
Create a c program that takes 1 parameter, a number using that number, dynamically allocate a...
Create a c program that takes 1 parameter, a number using that number, dynamically allocate a memory so you store that number of integers write integers in order starting from 1 until you fill all that memory print the address and values of the first and the last integer stored in the memory
a) Write C code initialize an array of ints to the last four digits of your...
a) Write C code initialize an array of ints to the last four digits of your phone number. Use a loop to add the digits. Calculate and display the average of the digits. b) Write C code using a struct to hold student information: integer id integer number of hours taken integer number of hours passed double gpa
A one Megabytes block of memory is allocated using the buddy system. Show the results of...
A one Megabytes block of memory is allocated using the buddy system. Show the results of the following sequence of requests and returns in a figure that is similar to Figure 7.6: Request A: 200 Kilobytes; Request B: 80 Kilobytes; Request C: 380 Kilobytes; Return A; Request D: 120 Kilobytes; Return B; Return D; Return C. Also, find the internal fragmentation at each stage of allocation/de-allocation.
This class has two constructors. The default constructor (the one that takes no arguments) should initialize the first and last names to "None", the seller ID to "ZZZ999", and the sales total to 0.
For this assignment, implement and use the methods for a class called Seller that represents information about a salesperson.The Seller classUse the following class definition:class Seller { public:   Seller();   Seller( const char [], const char[], const char [], double );        void print();   void setFirstName( const char [] );   void setLastName( const char [] );   void setID( const char [] );   void setSalesTotal( double );   double getSalesTotal(); private:   char firstName[20];   char lastName[30];   char ID[7];   double salesTotal; };Data MembersThe data members for the class are:firstName holds the Seller's first namelastName holds the Seller's last nameID holds the Seller's id numbersalesTotal holds the Seller's sales totalConstructorsThis class has two constructors. The default constructor (the one that takes...
Your Array class must provide the following features: --1.1) (20' pts) Two constructors. One takes the...
Your Array class must provide the following features: --1.1) (20' pts) Two constructors. One takes the input of the initialized capacity (int) and create the underlying array with that capacity; the other takes no input and creates an array that can hold a single element by default (that is, init capacity == 1); --1.2) (20' pts) Getters and setters and basic methods we discussed during the class including: getCapacity(), getSize(), set(int index, E val), get(int index), isEmpty(); --1.3) (20' pts)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT