c++ - 调用一个方法,并传入一个指向类的指针

标签 c++

我正在尝试调用一个方法来将一个对象添加到另一个对象中的 vector 中。我收到错误;

'': Illegal use of this type as an expression

在我的程序中,我声明了一个对象来将我的节点存储在主程序中;

accountStream *accountStore = new accountStream;

然后调用函数;

new_account(&accountStore);

new_account 函数如下;

void new_account(accountStream &accountStorage)
{
    newAccount *account = new newAccount;
    (&accountStorage)->pushToStore(account);
}

帐户流类有一个接收它的 vector ,但这是我的错误所在;

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);
}

错误在倒数第二行;

accountStore.push_back(account);

我感觉这与我将对象传递给方法的方式有关,但在胡思乱想了一段时间后,我一直无法查明到底哪里出了问题。

最佳答案

2个问题:

  1. new_account(&accountStore);错误,使用new_account(*accountStore);匹配参数类型。

  2. accountStore.push_back(account); 是错误的。 account 类型不是对象。向函数添加一些参数。

关于c++ - 调用一个方法,并传入一个指向类的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38502470/

相关文章:

C++ -- 关于函数指针的问题

c++ - boost::erase_all 从字符串中删除多个字符

c++ - 如何将 Poco::SharedPtr 传递给 TimerCallback 函数?

c++ - 读取恰好具有良好值的未初始​​化变量

c++ - GHC 未链接依赖项

c++ - 在 vector 中存储各种类型

c++ - 访问指针 vector 中的信息

c++ - 是否可以通过 Windows C++ 应用程序的唯一地址检测到蓝牙设备?

c++ - 当 GetQueuedCompletionStatus() 返回 FALSE 时,这些参数的值是什么?

c++ - opencv waitkey,不精确,不起作用?