Whenever I try to compile a project in g++, I get the following errors (below). I have several other files for this, but I'm refraining from posting all of them to spare monitor-induced headache
I don't have the iterator assignment operator overloaded anywhere in my code, and furthermore, this compiles and runs perfectly under Visual Studio 2008. Does anyone know why this might be happening?
(The offending lines are both "iter1 = pList.begin()", in their respective functions).
Thanks!!
Code:
sortList.h: In function void outputListByTotalProfit(const std::list<popClass, std::allocator<popClass> >&):
sortList.h:41: error: no match for operator= in iter1 = ((const std::list<popClass, std::allocator<popClass> >*)pList)->std::list<_Tp, _Alloc>::begin [with _Tp = popClass, _Alloc = std::allocator<popClass>]()
/usr/include/c++/4.3/bits/stl_list.h:112: note: candidates are: std::_List_iterator<popClass>& std::_List_iterator<popClass>::operator=(const std::_List_iterator<popClass>&)
sortList.h: In function void outputListByProfitMargin(const std::list<popClass, std::allocator<popClass> >&):
sortList.h:125: error: no match for operator= in iter1 = ((const std::list<popClass, std::allocator<popClass> >*)pList)->std::list<_Tp, _Alloc>::begin [with _Tp = popClass, _Alloc = std::allocator<popClass>]()â
/usr/include/c++/4.3/bits/stl_list.h:112: note: candidates are: std::_List_iterator<popClass>& std::_List_iterator<popClass>::operator=(const std::_List_iterator<popClass>&)
Code:
#ifndef __SORTLIST__H
#define __SORTLIST__H
#include <list>
#include <vector>
#include <string>
#include <fstream>
#include <cstdlib>
#include "popClass.h"
using namespace std;
void outputListByTotalProfit(const list<popClass>& pList); //Sort by total profit. Precondition: none. Postcondition: outputs the recommendation by total profit to recommendation.dat
void outputListByProfitMargin(const list<popClass>& pList); //Sort by profit margin. Precondition: none. Postcondition: outputs the recommendation by profit margin to recommendation.dat
//Sort by total profit. Precondition: none. Postcondition: outputs the recommendation by total profit to recommendation.dat
void outputListByTotalProfit(const list<popClass>& pList)
{
list<popClass>::iterator iter1; //Iterator used for traversing the list
int outerloop=0, innerloop=0; //incrementing integers used for looping
int totalsize = pList.size(); //total number of elements in list
int count = 0; //Used for offset of arrays
string *names = NULL; //Dynamic array of strings derived from popName of popClass objects
float *profit = NULL; //Dynamic array of total profit derived from popClass objects
string swapName = ""; //Used for swapping during bubblesort
float swapProfit = 0; //Used for swapping during bubblesort
ofstream outFile("recommendations.dat", ios::app); //File used for recommendations
//check to see if the file is good
if (outFile.bad())
{
cout << "Cannot open recommendations.dat. Terminating program" << endl;
exit(1);
}
//Allocate memory for string and profit lists
names = new string [totalsize];
profit = new float [totalsize];
//Begin copying name and profits from pList
iter1 = pList.begin();
//Copies the name and profit from pList
for (iter1; iter1 != pList.end(); iter1++)
{
names[count] = iter1->getName();
profit[count] = iter1->totalProfit();
count++;
}
//Bubblesort: sorts from most profitable to least profitable
for (outerloop=0; outerloop < totalsize; outerloop++)
{
for (innerloop=0; innerloop < totalsize; innerloop++)
{
if (profit[outerloop] > profit[innerloop])
{
swapName = names[outerloop];
names[outerloop] = names[innerloop];
names[innerloop] = swapName;
swapProfit = profit[outerloop];
profit[outerloop] = profit[innerloop];
profit[innerloop] = swapProfit;
}
}
}
//Output the best soda header
outFile << "Best Soda by Total Profit (most profitible)" << endl
<< "-------------------------------------------" << endl
<< "Name of Soda" << "\t\t" << "Total Profit" << endl;
//Output the 10 most profitible pop
for (count = 0; count < 10; count++)
{
outFile << names[count] << "\t\t\t" << profit[count] << endl;
}
//Output the worst soda header
outFile << endl << endl << endl
<< "Worst Soda by Total Profit (least profitible)" << endl
<< "---------------------------------------------" << endl
<< "Name of Soda" << "\t\t\t" << "Total Profit" << endl;
//Output the 10 worst sodas in descending order
//Using [totalsize - count - 1] since [totalsize - 1] is the end of the array and need to iterate backwards
for (count = 0; count < 10; count++)
{
outFile << names[totalsize - count - 1] << "\t\t\t" << profit[totalsize - count - 1] << endl;
}
outFile << endl << endl << endl;
//Close the file
outFile.close();
outFile.clear();
}
//Sort by profit margin. Precondition: none. Postcondition: outputs the recommendation by profit margin to recommendation.dat
void outputListByProfitMargin(const list<popClass>& pList)
{
list<popClass>::iterator iter1; //Iterator used for traversing the list
int outerloop=0, innerloop=0; //incrementing integers used for looping
int totalsize = pList.size(); //total number of elements in list
int count = 0; //Used for offset of arrays
string *names = NULL; //Dynamic array of strings derived from popName of popClass objects
float *profit = NULL; //Dynamic array of total profit derived from popClass objects
string swapName = ""; //Used for swapping during bubblesort
float swapProfit = 0; //Used for swapping during bubblesort
ofstream outFile("recommendations.dat", ios::app); //File used for recommendations
//check to see if the file is good
if (outFile.bad())
{
cout << "Cannot open recommendations.dat. Terminating program" << endl;
exit(1);
}
//Allocate memory for string and profit lists
names = new string [totalsize];
profit = new float [totalsize];
//Begin copying name and profits from pList
iter1 = pList.begin();
//Copies the name and profit from pList
for (iter1; iter1 != pList.end(); iter1++)
{
names[count] = iter1->getName();
profit[count] = iter1->profitMargin();
count++;
}
//Bubblesort: sorts from most profitable to least profitable
for (outerloop=0; outerloop < totalsize; outerloop++)
{
for (innerloop=0; innerloop < totalsize; innerloop++)
{
if (profit[outerloop] > profit[innerloop])
{
swapName = names[outerloop];
names[outerloop] = names[innerloop];
names[innerloop] = swapName;
swapProfit = profit[outerloop];
profit[outerloop] = profit[innerloop];
profit[innerloop] = swapProfit;
}
}
}
//Output the best soda header
outFile << "Best Soda by Profit Margin (most profitible)" << endl
<< "-------------------------------------------" << endl
<< "Name of Soda" << "\t\t" << "Profit Margin" << endl;
//Output the 10 most profitible pop
for (count = 0; count < 10; count++)
{
outFile << names[count] << "\t\t\t" << profit[count] << endl;
}
//Output the worst soda header
outFile << endl << endl << endl
<< "Worst Soda by Profit Margin (least profitible)" << endl
<< "---------------------------------------------" << endl
<< "Name of Soda" << "\t\t\t" << "Profit Margin" << endl;
//Output the 10 worst sodas in descending order
//Using [totalsize - count - 1] since [totalsize - 1] is the end of the array and need to iterate backwards
for (count = 0; count < 10; count++)
{
outFile << names[totalsize - count - 1] << "\t\t\t" << profit[totalsize - count - 1] << endl;
}
outFile << endl << endl << endl;
outFile.close();
outFile.clear();
}
#endif