首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

在vector里面push_back一个自定义类型对象时出了有关问题

2012-02-26 
求助:在vector里面push_back一个自定义类型对象时出了问题classCAccount{private:intm_id//帐户编号float

求助:在vector里面push_back一个自定义类型对象时出了问题
class   CAccount
{

private:
int   m_id;                                       //帐户编号
float   m_balance;//帐户余额
char   m_sex[3];//帐户性别
string   m_name;//帐户名字
string   m_mobilephone;//帐户手机号码
DateType   m_bithday;//帐户生日



};


vector <CAccount>   m_allcount;           //当前所有帐户


void   CAccountManagement::AddAccount(CAccount   newaccount)
{
//加上这句话就出错  

m_allcount.push_back(newaccount);          
}

错误提示  
error   C2679:   binary   '= '   :   no   operator   defined   which   takes   a   right-hand   operand   of   type   'const   class   CAccount '   (or   there   is   no   acceptable   conversion)
                d:\program   files\microsoft   visual   studio\vc98\include\vector(170)   :   see   reference   to   function   template   instantiation   'void   __cdecl   std::fill(class   CAccount   *,class   CAccount   *,const   class   CAccount   &) '   being   compiled
Skipping...   (no   relevant   changes   detected)


象是没重载转换运算符的问题     但是这个转换要怎么写     将一个const   class转成class   ?




[解决办法]
你在类里没有定义了void AddAccount(CAccount newaccount);加进去你试试.
[解决办法]
楼主用什么编译器,我用gcc3.4.2没有问题的。
[解决办法]
楼主用的是VS吧: "microsoft visual studio\vc98\ "

方法的设计上也不太对:AddAccount并不是CAccount的一个行为,直接用为一个普通函数就行了,况且要作为类的方法,也没有在类的声明:
void AddAccount(CAccount newaccount)
{
//加上这句话就出错

m_allcount.push_back(newaccount);
}
[解决办法]
给你类添加拷贝构造和拷贝赋值并实现之.

class CAccount
{
public:
CAccount(const CAccount& rhs); //拷贝构造
CAccount& operator=(const CAccount& rhs); //拷贝赋值
........

};

CAccount::CAccount(const CAccount& rhs)
{
................
}

CAccount& CAccount::operator=(const CAccount& rhs)
{
.................
}

[解决办法]
AddAccount 是一个成员函数,
vector <CAccount> m_allcount; 是一个全局对象 ...

然后在成员函数中访问全局变量?

那还不如把 vector <CAccount> m_allcount; 定义为静态成员 ...

热点排行