如何使用参数传递对象的引用

时间:2015-11-15 05:08:04

标签: c++ debugging c++11 pass-by-reference

#include "Data.h"

#include "Heap.h"

#include "PQ.h"
#include <iostream>

using namespace std;

//**********************************************************

void enqRequest(int newTrackNum, int currP ,int serialNumber,bool 
forwardDir, itemClass &item(int &newTrackNum,int &serialNumber))

{

cout << "Do the EnqRequest function."<<endl;
cout <<endl<<endl;
cout <<"Enter a New Track Number"<<endl;
cin >> newTrackNum;

if (newTrackNum >= 0 && newTrackNum <= 9) {




 cout <<"EnqRequest   "<<newTrackNum<<endl;
 cout << endl << endl;
 cout <<"Current Position: .............."<<currP<<endl;
 cout <<"Current Direction: ............."<<endl;
 cout <<"Request was Enqueued in the: ..."<<endl;
 cout <<"Number of Request is: ......"<<endl;
 cout      <<"======================================================================    ======"<<endl;
    }
    else
        cout <<"Please Enter a number request between 0-9"<<endl;




}
//**********************************************************


int main(void) {


bool user = true;
bool fowardDir= true;
string commandUser;
int  trackNum, currentPosition,T;
T=0;

pqItemType forward;
pqItemType reverse;
itemClass item(int trackNum, int T);


currentPosition=0;
cout << "WELCOME TO THE DISK SCHEDULER"<<endl;
cout << "THE COMMANDS AVAILABLE TO YOU ARE \"EnqRequest\", \"ServeRequest\", \"PrintState\", AND \"Quit\"\n";
cin >> commandUser;
std::transform(commandUser.begin(), commandUser.end(), commandUser.begin(), ::toupper);

do{
    if ( commandUser == "ENQREQUEST" )
    {
        enqRequest(trackNum,currentPosition,T,fowardDir,&item);


        cout << "THANK YOU FOR USING THE DISK SCHEDULER"<<endl;
        cout << "THE COMMANDS AVAILABLE TO YOU ARE \"EnqRequest\", \"ServeRequest\", \"PrintState\", AND \"Quit\"\n";
        cin >> commandUser ;
        std::transform(commandUser.begin(), commandUser.end(), commandUser.begin(), ::toupper);
    }
  else
    {
        cout << "Please, enter a command" << endl;
        cin >> commandUser;
        std::transform(commandUser.begin(), commandUser.end(), commandUser.begin(), ::toupper);
    }

}while (user);


return 0;
}

我有一个带参数的对象项(int trackNum,int T);我试图将对象的引用传递给enqRequest函数和其他尚未创建的函数。当我传递对象引用时,我是否还必须专门传递对象本身使用的参数?例如

void enqRequest(itemClass &item(int &newTrackNum,int &serialNumber))

1 个答案:

答案 0 :(得分:1)

以下对象构造错误: itemClass item(int trackNum,int T);

它应该是:

itemClass item(trackNum, T);

这假设您有一个构造函数,它将trackNum,T参数分配给itemClass的类成员。

如果要传递对此对象的引用,则调用签名将为:

enqRequest(trackNum,currentPosition,T,fowardDir,item);

函数enqRequest需要是:

void enqRequest(int newTrackNum, int currP ,int serialNumber,bool 
forwardDir, itemClass &item)
{
cout << "Do the EnqRequest function."<<endl;
cout <<endl<<endl;
cout <<"Enter a New Track Number"<<endl;
cin >> newTrackNum;

if (newTrackNum >= 0 && newTrackNum <= 9) {
 cout <<"EnqRequest   "<<newTrackNum<<endl;
 cout << endl << endl;
 cout <<"Current Position: .............."<<currP<<endl;
 cout <<"Current Direction: ............."<<endl;
 cout <<"Request was Enqueued in the: ..."<<endl;
 cout <<"Number of Request is: ......"<<endl;
 cout      <<"======================================================================    ======"<<endl;
    }
    else
        cout <<"Please Enter a number request between 0-9"<<endl;




}