[英]Passing Vector from one class to another for manipulation
我想得到向量
std::vector< std::vector<int> > Ticket;
從一個類(用戶)到另一個類(游戲),以便在那里操作內容。
具體來說,我需要一個函數(compareNumbers)。
當前在main.cpp中,我有以下代碼:
#include "User.h"
#include "Game.h"
int k = game.compareNumbers(user.<int>Ticket);
(為簡化起見,省略了不必要的包含和其他代碼)
在User.h中是矢量的聲明(公共),在User.cpp中是Vector的聲明。
我懷疑它更復雜,因為它是一個包含向量的向量,但我不確定。
在Game.cpp中,我有
int Game::compareNumbers(std::vector< std::vector<int> > Ticket)
// Some code
我遇到的錯誤是main中第一個<上的“預期的成員名稱”。 但是我懷疑還有更多的錯誤。
另外,我想也許我應該參考一下,但是我不確定。 而且我不確定該怎么做,甚至在比較數中也可能是const,我不是在修改向量,只是將其內容與另一個向量的內容進行比較,還是不確定嗎?
假設您的user
具有Ticket
成員,則可以使用以下方法將此對象傳遞給Game::compareNumbers()
函數
int = game.compareNumbers(user.Ticket);
聲明之后,這將復制std::vector<std::vector<int> >
,但這可能並不是您想要的。 如果您真的只想看一下數字(函數的名稱表明您的所作所為),則可以使用const&
傳遞對象:
int Game::compareNumbers(std::vector<std::vector<int> > const& Ticker) {
// ...
}
如果compareNumbers()
實際上需要修改其參數,則可以省略const
但保留引用:如果沒有引用,則會傳遞並修改副本,這不會影響原始的user.Ticket
對象。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.