| |

VerySource

 Forgot password?
 Register
Search
View: 743|Reply: 7

Problems using list class in STL

[Copy link]

4

Threads

16

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 China

Post time: 2020-1-20 07:40:02
| Show all posts |Read mode
I have defined a commodity class:
class CMerchandise
{
public:
   CMerchandise ();
   CMerchandise (string goodsname, string modnum,\
 string goodsnum, int count, double price, string provider, string manname);
   ~ CMerchandise ();
   void SetGoodsName (string goodsname);
   void SetModelNum (string modnum);
   void SetGoodsNum (string goodsnum);
   void SetCount (int count);
   void SetPrice (double price);
   void SetProfits (double profits);
   void SetManName (string manname);
   void SetProvider (string provider);

   string GetGoodsName () const;
   string GetModelNum () const;
   string GetGoodsNum () const;
   string GetManName () const;
   string GetProvider () const;
   int GetCount () const;
   double GetPrice () const;
   double GetProfits () const;
    
   // Overload the "<" operator for easy STL sorting
   bool operator <(const CMerchandise&m) const
  {
return m_nCount> m.m_nCount; // From largest to smallest according to the number of products
   }
Ranch
private:
    string m_strGoodsName; // Item name
    string m_strModelNum; // Product model
    string m_strGoodsNum; // Article number
    int m_nCount; // Number of products
    double m_dwPrice; // commodity purchase price
    double m_dwProfits; // commodity profit ()
    string m_strProvider; // Provider
    string m_strManName; // Person in charge of goods (purchase or sale)
};

typedef list <CMerchandise> MerchandiseList; // Commodity chain
I use the following statement in another class:
Where m_MerchandiseList is defined like this: MerchandiseList m_MerchandiseList;

.........

MerchandiseList * pMerchandiseList = new MerchandiseList;

for (MerchandiseListIte = m_MerchandiseList.begin ();
MerchandiseListIte! = M_MerchandiseList.end (); MerchandiseListIte ++) <-error !!
{
 if ((* MerchandiseListIte) .GetGoodsName () == Merchandise.GetGoodsName ()&&
(* MerchandiseListIte) .GetGoodsNum () == Merchandise.GetGoodsNum ()&&
(* MerchandiseListIte) .GetManName () == Merchandise.GetManName ())
 {
pMerchandiseList-> push_back (* MerchandiseListIte);
bFlag = true;
 }
}

For statement is an error when compiling! I don't know what the reason, please prawn pointing, urgent! !! !!

error C2679: binary '=': no ​​operator defined which takes a right-hand operand of type 'class std :: list <class CMerchandise, class std :: allocator <class CMerchandise>> :: const_iterator' (or there is no acce
Reply

Use magic Report

0

Threads

49

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-1-29 09:00:01
| Show all posts
m_MerchandiseList.begin (); gets iterator instead of pointer, change it to this:

for (MerchandiseList :: iterator MerchandiseListIte = m_MerchandiseList.begin ();
MerchandiseListIte! = M_MerchandiseList.end (); MerchandiseListIte ++)
{
 if ((* MerchandiseListIte) .GetGoodsName () == Merchandise.GetGoodsName ()&&
(* MerchandiseListIte) .GetGoodsNum () == Merchandise.GetGoodsNum ()&&
(* MerchandiseListIte) .GetManName () == Merchandise.GetManName ())
 {
pMerchandiseList-> push_back (* MerchandiseListIte);
bFlag = true;
 }
}
Reply

Use magic Report

0

Threads

49

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-1-29 09:45:01
| Show all posts
Iterator just behaves like a pointer, specifically whether the pointer depends on the implementation of the library
Reply

Use magic Report

4

Threads

16

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 China

 Author| Post time: 2020-1-29 13:00:01
| Show all posts
Please look upstairs, I forgot to say, it should have:
MerchandiseList :: iterator MerchandiseListIte; The reason is not the problem you said.
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 Invalid IP Address

Post time: 2020-1-29 14:36:01
| Show all posts
I do n’t know if your MerchandiseListIte definition is wrong or what is going on. I do n’t have a problem here. The test code is as follows:
#include <iostream>
#include <string>
#include <list>
#include <vector>
using namespace std;

class CMerchandise
{
public:
CMerchandise ();
CMerchandise (string goodsname, string modnum,\
string goodsnum, int count, double price, string provider, string manname);
~ CMerchandise ();
void SetGoodsName (string goodsname);
void SetModelNum (string modnum);
void SetGoodsNum (string goodsnum);
void SetCount (int count);
void SetPrice (double price);
void SetProfits (double profits);
void SetManName (string manname);
void SetProvider (string provider);
Ranch
string GetGoodsName () const;
string GetModelNum () const;
string GetGoodsNum () const;
string GetManName () const;
string GetProvider () const;
int GetCount () const;
double GetPrice () const;
double GetProfits () const;
Ranch
// Overload the "<" operator for easy STL sorting
bool operator <(const CMerchandise&m) const
{
return m_nCount> m.m_nCount; // From largest to smallest according to the number of products
}

private:
string m_strGoodsName; // Item name
string m_strModelNum; // Product model
string m_strGoodsNum; // Article number
int m_nCount; // Number of products
double m_dwPrice; // commodity purchase price
double m_dwProfits; // commodity profit ()
string m_strProvider; // Provider
string m_strManName; // Person in charge of goods (purchase or sale)
};

int main ()
{
bool bFlag = false;
typedef list <CMerchandise> MerchandiseList;
MerchandiseList m_MerchandiseList;
CMerchandise Merchandise;
MerchandiseList * pMerchandiseList = new MerchandiseList;
list <CMerchandise> :: iterator MerchandiseListIte;
for (MerchandiseListIte = m_MerchandiseList.begin ();
MerchandiseListIte! = M_MerchandiseList.end ();
MerchandiseListIte ++)
{
if ((* MerchandiseListIte) .GetGoodsName () == Merchandise.GetGoodsName ()&&
(* MerchandiseListIte) .GetGoodsNum () == Merchandise.GetGoodsNum ()&&
(* MerchandiseListIte) .GetManName () == Merchandise.GetManName ())
{
pMerchandiseList-> push_back (* MerchandiseListIte);
bFlag = true;
}
}
}
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 Canada

Post time: 2020-1-30 01:27:01
| Show all posts
Check to see if your member or member function has a const qualifier
Try it with MerchandiseList :: const_iterator
Reply

Use magic Report

4

Threads

16

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 China

 Author| Post time: 2020-1-30 09:54:01
| Show all posts
intoblue00Brother, as you said, I did use the const member function in my class a lot. I was relieved (I can't really check the error ^ _ ^ at the beginning), and other files have not been compiled.
Reply

Use magic Report

0

Threads

49

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-1-30 20:18:02
| Show all posts
Haha, that's really embarrassing
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list