2016-07-21 7 views
0

私は別のオブジェクト内のベクトルにオブジェクトに追加するメソッドを呼び出そうとしています。私はエラーを取得しています。メソッドを呼び出して、クラスへのポインタを渡す

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

アカウントストリームクラスにはそれを受け取るベクトルがありますが、どこにエラーがありますか。

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

エラーは最後の2行目です。私はそれは私がメソッドにオブジェクトを渡している方法とは何かだ気持ちを持っているが、しばらくの間、周りいじりの後、私は私がきた場所を正確に突き止めることができていない

accountStore.push_back(account); 

間違った。

答えて

0

いくつかの問題:あなたは、ポインタ(ないへの参照を受信する必要があり

template<class account> 
inline void accountStream::pushToStore(account c) 
{ 
    accountStore.push_back(c); 
} 
    1. 現在の変数名(とないタイプのみ)を指定する必要がありますポインタ)ここに

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

      コール機能:

      new_account(accountStore); 
      
      あなたは変数(ないポインタ)を宣言することができ、また

      new_account(accountStore); 
      

      あなたは、パラメータとしてポインタで関数を呼び出す必要があります

      と参照を受け取る:

      void new_account(accountStream &accountStorage) 
      { 
          newAccount *account = new newAccount; 
          accountStorage.pushToStore(account); 
      } 
      
    3. 関数は(あなたはポインタに&演算子を使用することから得るものです)ポインタへのポインタを参照を取得していないので
  • +0

    ありがとうございます! – Dannys19

    1

    2問題:

    1. new_account(&accountStore);引数の型と一致するようにnew_account(*accountStore);を使用し、間違っています。

    2. accountStore.push_back(account);が間違っています。 accountはオブジェクトではないオブジェクトです。関数に引数をいくつか追加します。

    0

    としては、& accountStoreをaccountStore *あなたが使用する必要があり、すでにここに答えていません。

    第二の問題ここにある:

    template<class account> 
    inline void accountStream::pushToStore(account) 
    { 
        accountStore.push_back(account); 
    } 
    

    あなたは「アカウント」にテンプレート関数を宣言しているが、そのためアカウントが型であり、あなたが次の行にやろうとしていることは一back型で、オブジェクトではありません。 正しいコードは次のようになります

    template<class account> 
    inline void accountStream::pushToStore(account acct) 
    { 
        accountStore.push_back(acct); 
    } 
    

    ACCTタイプアカウントのインスタンスであるアカウントがタイプからです。

    +1

    ええ、人々はそれが私がちょっとばかげた気分にさせたと説明した後!私はいくつかの読書を持っていると思います。 – Dannys19

    関連する問題