我正在创建一个程序,我将类对象存储在向量中,并将此向量作为const向量作为引用变量传递给友元函数,并使用const_vector进行迭代,我试图调用类对象的成员函数使用const_iterator所以,我得到错误。但是当我这样做没有const vector并且没有const_iterator迭代它时我的程序运行成功。我们不能迭代const_vector或者我们不能使用const_vector访问成员变量或函数。
vector<AccountHolder>::iterator searchAccount(const vector<AccountHolder>& v,long int accno)
{
vector<AccountHolder>::const_iterator itr;
for(itr = v.begin();itr!=v.end();itr++)
{
if(itr->getAccNo() == accno)return itr;
}
return v.end();
}
这是整个代码。
#include<iostream>
#include<vector>
using namespace std;
class Account{
private:
long int AccNo;
char AccType;
public:
long int getAccNo()
{
return AccNo;
}
char getAccType()
{
return AccType;
}
void setAccNo(long int accno)
{
AccNo = accno;
}
void setAccType(char acctype)
{
AccType = acctype;
}
};
class AccountHolder:public Account{
private:
string Name;
public:
string getAccHolderName()
{
return Name;
}
void setAccHolderInfo(long int accno,char acctype,string name)
{
setAccNo(accno);
setAccType(acctype);
Name = name;
}
void displayAccountHolderInfo()
{
cout << "Name : " << getAccHolderName() << endl;
cout << "Account Number : " << getAccNo() << endl;
cout << "Account Type : " << getAccType() << endl;
}
friend void displayAllAccountHolder(vector<AccountHolder> &v);
friend vector<AccountHolder>::iterator searchAccount(vector<AccountHolder> &v,long int accno);
//friend bool searchAccount(const vector<AccountHolder> &v,long int accno,char acctype);
//friend vector<AccountHolder>::iterator searchAccount(const vector<AccountHolder> &v,long int accno=0,string name);
};
void displayAllAccountHolder(vector<AccountHolder> &v)
{
for(vector<AccountHolder>::iterator itr = v.begin();itr != v.end() ; itr++)
itr->displayAccountHolderInfo();
}
vector<AccountHolder>::iterator searchAccount(const vector<AccountHolder>& v,long int accno)
{
vector<AccountHolder>::const_iterator itr;
for(itr = v.begin();itr!=v.end();itr++)
{
if(itr->getAccNo() == accno)return itr;
}
return v.end();
}
int main()
{
vector<AccountHolder> Acc;
vector<AccountHolder> SavingAcc;
vector<AccountHolder> CurrentAcc;
vector<AccountHolder>::iterator temp;
AccountHolder AccH;
long int accno;
char acctype;
string name;
do{
cout << "Select Your Choice : " << endl;
cout << "1. Insert Data " << endl;
cout << "2. Display Data " << endl;
cout << "3. Display All User Data " << endl;
cout << "4. Exit " << endl;
cout << " Enter Your Choice : ";
int choice;
cin >> choice;
switch(choice)
{
case 1: cout << "\nEnter Name : ";
cin.get();
getline(cin >> ws,name);
accNo:
cout << "\nEnter Account Number : ";
cin >> accno;
if(searchAccount(Acc,accno)!=Acc.end())
{
cout << "\nAccount Number already exist." << endl;
goto accNo;
}
accType:
cout << "\nEnter Account Type : ";
cin >> acctype;
if(acctype == 's' || acctype =='S')
{
AccH.setAccHolderInfo(accno,'S',name);
SavingAcc.push_back(AccH);
Acc.push_back(AccH);
}
else if(acctype == 'c' || acctype == 'C')
{
AccH.setAccHolderInfo(accno,'C',name);
CurrentAcc.push_back(AccH);
Acc.push_back(AccH);
}
else
{
cout << "\n Invalid Account Type" << endl;
goto accType;
}
break;
case 2: cout << "\nEnter Account Number : ";
cin >> accno;
cout << "\nEnter Account Type(or just type 'n') : ";
cin.get();
cin >> acctype;
if(acctype == 's' || acctype == 'S')
temp = searchAccount(SavingAcc,accno);
else if(acctype == 'c' || acctype == 'C')
temp = searchAccount(CurrentAcc,accno);
else
temp = searchAccount(Acc,accno);
if(temp!=SavingAcc.end() && temp!=CurrentAcc.end() && temp!=Acc.end() )
temp->displayAccountHolderInfo();
else
cout << "\n Account does not exist. " << endl;
break;
case 3:displayAllAccountHolder(Acc);
break;
case 4: return 0;
break;
default:
break;
}
}while(true);
return 0;
}
但是当你将const vector移到vector时它会起作用。
vector :: iterator searchAccount(const vector&amp; v,long int accno)
答案 0 :(得分:0)
为了在常量对象上调用方法,必须将该方法声明为const
(以声明隐藏的this
参数为const
),并且不得尝试修改对象(因为this
是const
)。简单地不修改方法内部的对象是不够的。所以
long int getAccNo()
{
return AccNo;
}
必须是
long int getAccNo() const
{
return AccNo;
}
事实上,只要你有一个没有或不应该修改对象的函数,就应该用一般原则声明它const
。这使编译器更容易为您捕获错误,并且可以节省大量的调试时间。
vector<AccountHolder>::iterator searchAccount(const vector<AccountHolder>& v,long int accno)
返回vector<AccountHolder>::iterator
,但在您正在使用的方法中并尝试返回vector<AccountHolder>::const_iterator
。这些是不同的,不兼容的类型,因此必须更改其中一个。我推荐
vector<AccountHolder>::const_iterator searchAccount(const vector<AccountHolder>& v,long int accno)
因为const_iterator
无法避免const vector
。