[英]Calling a method, and passing in a pointer to class
I'm trying to call a method to add to an object to a vector within another object. 我试图调用一种方法,以将一个对象添加到另一个对象内的向量。 I'm getting the error;
我得到了错误;
'': Illegal use of this type as an expression
Within my program I declare an object to store my node in the main; 在我的程序中,我声明了一个将节点存储在主对象中的对象;
accountStream *accountStore = new accountStream;
Then call the function; 然后调用该函数;
new_account(&accountStore);
The new_account function is as; new_account函数为;
void new_account(accountStream &accountStorage)
{
newAccount *account = new newAccount;
(&accountStorage)->pushToStore(account);
}
The account stream class has a vector that receives it, but there is where my error is; 帐户流类有一个接收它的向量,但是这里是我的错误所在。
class accountStream
{
public:
accountStream();
~accountStream();
template <class account>
void pushToStore(account);
private:
std::vector <newAccount*> accountStore;
};
template<class account>
inline void accountStream::pushToStore(account)
{
accountStore.push_back(account);
}
The error is on the second last line; 错误在倒数第二行;
accountStore.push_back(account);
I've got a feeling it's something to do with the way I'm passing the object into the method, but after messing around for a while I haven't been able to pinpoint where exactly I've gone wrong. 我有种感觉,这与将对象传递给方法的方式有关,但是在搞乱了一段时间之后,我一直无法查明我到底在哪里出错。
2 problems: 2个问题:
new_account(&accountStore);
is wrong, use new_account(*accountStore);
是错误的,请使用
new_account(*accountStore);
to match the argument type. 匹配参数类型。
accountStore.push_back(account);
is wrong. 是错的。
account
is type not object. account
类型不是对象。 Add some argument to the function. 向该函数添加一些参数。
Several issues: 几个问题:
You must specify the variable name here (and not only the type): 您必须在此处指定变量名称(不仅是类型):
template<class account> inline void accountStream::pushToStore(account c) { accountStore.push_back(c); }
You must receive a pointer (not a reference to a pointer) here 您必须在此处收到一个指针(不是对指针的引用)
void new_account(accountStream *accountStorage) { newAccount *account = new newAccount; accountStorage->pushToStore(account); }
You must call the function with a pointer as a parameter: 您必须使用指针作为参数来调用该函数:
new_account(accountStore);
Alternatively, you can declare the variable (not a pointer to): 另外,您可以声明变量(不是指针):
accountStream accountStore;
call the function: 调用函数:
new_account(accountStore);
and receive a reference: 并获得参考:
void new_account(accountStream &accountStorage) { newAccount *account = new newAccount; accountStorage.pushToStore(account); }
As answered here already, you need to use *accountStore and not &accountStore because the function takes a reference and not a pointer to a pointer (which is what you get from using & operator on a pointer). 正如这里已经回答的那样,您需要使用* accountStore而不是&accountStore,因为该函数接受引用而不是指向指针的指针(这是通过对指针使用&运算符获得的)。
the second problem is here: 第二个问题在这里:
template<class account>
inline void accountStream::pushToStore(account)
{
accountStore.push_back(account);
}
you are declaring the function templated on the 'account' therefore account is a type, and what you are trying to do in the next line is push_back a type and not an object. 您声明的是在'account'上模板化的函数,因此account是一种类型,而您在下一行尝试执行的操作是push_back是一种类型,而不是对象。 the correct code would be:
正确的代码是:
template<class account>
inline void accountStream::pushToStore(account acct)
{
accountStore.push_back(acct);
}
because account is the type while acct is an instance of the type account. 因为account是类型,而acct是类型account的实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.