C++ C++;如何将引用从一个函数传递到另一个函数?
我有下一个功能:C++ C++;如何将引用从一个函数传递到另一个函数?,c++,function,pointers,parameters,reference,C++,Function,Pointers,Parameters,Reference,我有下一个功能: void UserInterface::showMenu(vector<GraphicsCard> *allCards) { int currentCard = 0; string userInput = ""; cout << "1: Show Previous, 2: Show Next" << endl; cin >> userInput; switch (stoi(userInp
void UserInterface::showMenu(vector<GraphicsCard> *allCards) {
int currentCard = 0;
string userInput = "";
cout << "1: Show Previous, 2: Show Next" << endl;
cin >> userInput;
switch (stoi(userInput)) {
case 1:
if (currentCard > 0) {
currentCard--;
}
UserInterface::showGraphicsCardTable(&allCards[currentCard]);
UserInterface::showMenu(allCards);
break;
case 2:
if (currentCard < allCards->size()) {
currentCard++;
}
UserInterface::showGraphicsCardTable(&allCards[currentCard]);
UserInterface::showMenu(allCards);
break;
default:
break;
}
}
void用户界面::显示菜单(矢量*所有卡){
int currentCard=0;
字符串userInput=“”;
cout用户输入;
开关(stoi(用户输入)){
案例1:
如果(当前卡>0){
当前卡--;
}
用户界面::showGraphicsCardTable(&allCards[currentCard]);
用户界面::显示菜单(所有卡);
打破
案例2:
如果(currentCardsize()){
currentCard++;
}
用户界面::showGraphicsCardTable(&allCards[currentCard]);
用户界面::显示菜单(所有卡);
打破
违约:
打破
}
}
我试图将对向量特定元素的引用传递给void UserInterface::showGraphicsCardTable(GraphicsCard*card)
函数。问题是&所有卡[currentCard]
在这种情况下不起作用。
如何进一步传递引用?更改此选项:
UserInterface::showGraphicsCardTable(&allCards[currentCard]);
为此:
UserInterface::showGraphicsCardTable(&((*allCards)[currentCard]));
顺便问一下,为什么要传入指针?在
showMenu()
中传递引用 你问的是如何传递引用,而实际上你是在传递指针。只需将函数更改为vector&和GraphicsCard&。同时,将两条重复的线路移出交换机也不会有任何伤害。@stijn谢谢!我想我需要了解它们之间的区别。另外,关于重复行,你是对的。但是,菜单中将有更多选项。:)@斯蒂恩:很抱歉,我刚刚明白你所说的复制行是什么意思,我一定会这么做。:)谢谢你的建议!:)@W.F.我的错,我以为我按了按钮。:)