友元函数编译问题

时间:2008-05-29 14:37:19   来源:论坛整理  作者:  编辑:chinaitzhe
本人是初学者,共三个文件
queue.h

#include <iostream>
using namespace std;

class BankQueue;

class Customer{

private:
int _account;
int _amount;

public:
friend void BankQueue::DelQueue();
friend void BankQueue::Print();

Customer(int account = -1,int amount = 0)
:_account(account),_amount(amount){};

int GetAccount(){
return _account;
}

int GetAmount(){
return _amount;
}
};
const int ARRAY_SIZE = 100;

class BankQueue{
public:
BankQueue();

void EnQueue(Customer newElem);

Customer DelQueue();

int GetLength(){
return length;
}

void Print() const;

private:
Customer elem[ARRAY_SIZE];
int first;
int length;
};
--------------------------------------------
queue.cpp

#include "stdafx.h"
using namespace std;
#include "queue.h"


BankQueue::BankQueue()
{
first = length = 0;
}

void BankQueue::EnQueue(Customer newElem)
{
int pos = (first length) % ARRAY_SIZE;
elem[pos] = newElem;
length ;
}

Customer BankQueue::DelQueue()
{
Customer ret(elem[first]._account,elem[first]._amount);
first = (first 1) % ARRAY_SIZE;
length--;
return ret;
}

void BankQueue::Print() const
{
int pos = first;
cout < < "Queue:" < < endl;
for(int i = 0;i <length;i )
{
cout < < "account:" < < elem[pos]._account < <
"amount:" < <elem[pos]._amount < < endl;
pos = (pos 1) % ARRAY_SIZE;
}
cout < < endl;
}
--------------------------------------------
main.cpp

#include "stdafx.h"
using namespace std;
#include "queue.h"


int main()
{
BankQueue q;
int i;
for(i = 0;i < 6;i )
{
q.EnQueue(Customer(i,20 * i));
}
q.Print();

Customer a;
for(i = 0;i < 3;i )
{
a = q.DelQueue();
cout < < "Out queue:" < < a.GetAccount() < < " "
< < a.GetAmount() < < endl;
}

cout < < endl;
q.Print();
return 0;
}
网友回复:什么错误?
vc6?把友元写到类里试试
网友回复:这能编译通过么?
是不是应该这样?
C/C code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/





#include <iostream> 

using namespace std; 



const int ARRAY_SIZE = 100; 



class BankQueue{ 

public: 

BankQueue(); 



void EnQueue(Customer newElem); 



Customer DelQueue(); 



int GetLength(){ 

return length; 

} 



void Print() const; 



private: 

Customer elem[ARRAY_SIZE]; 

int first; 

int length; 

}; 



class Customer{ 



private: 

int _account; 

int _amount; 



public: 

friend void BankQueue::DelQueue(); 

friend void BankQueue::Print(); 



Customer(int account = -1,int amount = 0) 

:_account(account),_amount(amount){}; 



int GetAccount(){ 

return _account; 

} 



int GetAmount(){ 

return _amount; 

} 

}; 






网友回复:友元声明是有顺序的,非凡是在声明友元函数,友元类,友元类里面的函数的时候
网友回复:我也认为是声明的问题,不过不会改,请高手改写
网友回复:支持搂主,收藏
关键字:函数,编译,问题,

文章评论

共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面