Compile error with g++

This is a discussion on Compile error with g++ within the C++ Programming forums, part of the General Programming Boards category; Whenever I try to compile a project in g++, I get the following errors (below). I have several other files ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    29

    Compile error with g++

    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

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Your list is declared as const, so you need to use a const_iterator instead of a regular iterator. Also, make sure all the functions called through this iterator are declared as const too.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    I think it's because you're trying to use an ordinary iterator to iterate over a const container. Take this example:
    Code:
    #include <iostream>
    #include <vector>
    
    int main() {
        std::vector<int> v;
    
        v.push_back(4);
        v.push_back(2);
    
        const std::vector<int> &c = v;
    
        for(std::vector<int>::const_iterator i = c.begin(); i != c.end(); ++i) {
            std::cout << *i << "\n";
        }
    
        return 0;
    }
    If you change "const_iterator" to just "iterator", you get a compiler error too.

    [edit] Two minutes too slow. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C and C++ compile speed
    By swgh in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 01-02-2007, 01:37 PM
  2. Compile as you type
    By Rocketmagnet in forum A Brief History of Cprogramming.com
    Replies: 33
    Last Post: 12-07-2006, 12:36 PM
  3. How to compile mfc libs from platform sdk
    By tjcbs in forum Windows Programming
    Replies: 6
    Last Post: 11-19-2006, 07:20 AM
  4. Compile crashes certain windows
    By Loduwijk in forum C++ Programming
    Replies: 5
    Last Post: 03-26-2006, 08:05 PM
  5. How can I compile C or C++ with Visual Studio .NET?
    By Dakkon in forum C Programming
    Replies: 8
    Last Post: 02-11-2003, 01:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21