我要讲一词多义了。当我调用函数时,我遇到了向量的麻烦。这是我的代码:
Class Customer:
#pragma once
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class Customer
{
protected:
string id;
float money;
public:
Customer();
~Customer();
virtual void Input();
virtual void Output();
string GetId()
{
return id;
}
void SetId(string ID)
{
id = ID;
}
};
类LoyalCustomer: #pragma once
#include"Customer.h"
class LoyalCustomer:public Customer
{
int level; //level of relationship
public:
LoyalCustomer();
~LoyalCustomer();
void Input();
void Output();
};
RegularCustomer:
#pragma once
class RegularCustomer:public Customer
{
public:
RegularCustomer();
~RegularCustomer();
void Input();
void Output();
};
类超市:
#pragma once
#include"LoyalCustomer.h"
#include"RegularCustomer.h"
class SuperMarket
{
vector<Customer*> list;
public:
SuperMarket();
~SuperMarket();
void FindCustomer()
{
string ID;
cout << "Input id of customer: ";
cin >> ID;
for (int i = 0; i<list.size(); i++)
if (ID == list[i]->GetId())
{
//do something
}
}
void Input()
{
string ID;
cout << "Input id of customer: ";
cin >> ID;
Customer *p = NULL;
if (ID[0] == 'L')
{
p = new LoyalCustomer;
p->Input();
p->SetId(ID);
list.push_back(p);
}
if (ID[0] == 'R')
{
p = new RegularCustomer;
p->Input();
p->SetId(ID);
list.push_back(p);
}
}
void Output()
{
//printf customer
}
};
当我在FindCustomer()函数中调用GetID()函数时:if (ID == list[I]-> GetID()),然后我运行我的代码,程序不会通知错误,但我输入"ID"来查找,它不会发现。我不知道该怎么补救。请帮帮我。谢谢!
尝试比较ID.compare(list[i]->GetId()) == 0