Reputation: 21
I have been having trouble with the last bit of my code. I declared an instance of Grocery using the parameterized, copy and default constructor and made use of the operator= and operator<< overload. I am now having difficulty trying to create a dynamic array. I have to fill the array with the contents of my text filefileName.txt.
When I run this code I keep getting this one error: Error C2679 binary '>>': no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion). Can I not access the setters in the array using >> (w/o overloading)? If not, how can I?
int main()
{
// Parameter - Instance 1
Grocery g1("NA", 0, "NA");
g1.setName("Milk");
g1.setQuant("1");
g1.setUnit("Bottle");
Grocery g2(g1); // Calls copy constructor
// Default constructor - Instance 3
//Grocery g3();
// Operator =
Grocery g4;
cout << "Operator = Running" << endl;
g4 = g2;
cout << g4.getName() << endl;
cout << g4.getQuant() << endl;
cout << g4.getUnit() << endl << endl;
// Operator <<
cout << "Operator<< Running" << endl;
Grocery g5("Salt", "1", "Bag");
cout << g5 << endl;
//cout << g5.getName();
//cout << g5.getQuant();
//cout << g5.getUnit();
// Dynamic Array of Grocery
Grocery* groceryArray;
groceryArray = new Grocery[3];
ifstream inputFile;
inputFile.open("fileName.txt");
for (int i = 0; i < 3; i++)
{
inputFile >> groceryArray[i].setName; // LINE ERROR IS HERE**
}
inputFile.close();
delete[]groceryArray;
return 0;
}
//Grocery.h
#pragma once
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Grocery
{
private:
string* m_name;
string* m_quant;
string* m_unit;
public:
Grocery(); // Default constructor
Grocery(string n, string q, string u); // Parametered constructor
~Grocery(); // Destructor
Grocery(const Grocery& rhs); // Copy constructor
Grocery& operator=(const Grocery& rhs); // Operator=
friend ostream& operator<<(ostream& out, const Grocery& rhs); //Operator>>
string getName();
void setName(string n);
string getQuant();
void setQuant(string q);
string getUnit();
void setUnit(string u);
};
#include "Grocery.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
Grocery::Grocery() // Default constructor
{
m_name = new string;
m_quant = new string;
m_unit = new string;
*m_name = "N/A";
*m_quant = "NA";
*m_unit = "N/A";
}
Grocery::Grocery(string n, string q, string u) // Parameterized constructor
{
//cout << "3 parameter constructor called" << endl;
// Initializes member variables as parameter variables
m_name = new string;
m_quant = new string;
m_unit = new string;
*m_name = n;
*m_quant = q;
*m_unit = u;
}
Grocery::Grocery(const Grocery& rhs) // Copy
{
m_name = new string;
m_quant = new string;
m_unit = new string;
*m_name = *rhs.m_name;
*m_quant = *rhs.m_quant;
*m_unit = *rhs.m_unit;
}
Grocery& Grocery::operator=(const Grocery& rhs) // Operator=
{
// Performs deep copy of other Grocery instance
if (this == &rhs)
return *this;
*m_name = *rhs.m_name;
*m_quant = *rhs.m_quant;
*m_unit = *rhs.m_unit;
return *this;
}
ostream& operator<<(ostream& out, const Grocery& rhs) // Operator<<
{
out << *rhs.m_name << endl << *rhs.m_quant << endl << *rhs.m_unit << endl;
return out;
}
Grocery::~Grocery() // Destructor
{
delete m_name;
delete m_quant;
delete m_unit;
*m_name = nullptr;
*m_quant = nullptr;
*m_unit = nullptr;
}
string Grocery::getName() { return *m_name; }
void Grocery::setName(string n) { *m_name = n; }
string Grocery::getQuant() { return *m_quant; }
void Grocery::setQuant(string q) { *m_quant = q; }
string Grocery::getUnit() { return *m_unit; }
void Grocery::setUnit(string u) { *m_unit = u; }
Upvotes: 0
Views: 71
Reputation: 2914
First things first: groceryArray[i].setName
is a method of Grocery
which makes the statement **inputFile >> groceryArray[i].setName;
highly nonsensical.
What you want instead is to first read in a string and then change the name like so:
std::string name;
**inputFile >> name;
groceryArray[i].setName(name);
Upvotes: 2