【问题标题】:How do I pass in a reference to an object with arguments如何传递对带有参数的对象的引用
【发布时间】:2016-02-16 10:40:21
【问题描述】:
#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))

【问题讨论】:

  • C 没有“通过引用”传递的能力。相反,传递相关变量的地址并让函数接收指向类型的指针。
  • @chux 这段代码不可能是 C(尽管标签刚刚被删除)。查看所有cins 和couts。这是 C++,可以通过引用传递。
  • 是的,我的意思是C++
  • 带参数的对象项是什么意思?类(及其对象)具有属性。
  • @crod 当你说“我有一个带有参数的对象项(int trackNum,int T);”你是在说itemClass item(int trackNum, int T);这一行吗?如果是这样...item 不是一个对象,它是一个函数,它接受 2 个ints 作为参数并返回一个itemClass。您在 main() 中声明该函数,这是合法但奇怪的,以及 probably not what you mean

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


【解决方案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;




}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-02
    • 1970-01-01
    • 1970-01-01
    • 2012-10-25
    • 1970-01-01
    • 2012-02-03
    • 1970-01-01
    相关资源
    最近更新 更多