2016-07-21 4 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); 
} 

오류는 마지막 두 번째 줄에 있습니다.

accountStore.push_back(account); 

나는 그것이 내가하는 방법으로 개체를 전달하고있어 길을 함께 할 수있는 뭔가의 느낌을 가지고 있지만, 잠시 동안 장난 후 나는 정확히 어디에 내가했습니다 찾아 낼 수 없었다 잘못했다.

답변

0

몇 가지 문제 :

  1. 당신은 여기에 (그리고뿐만 아니라 타입) 변수 이름을 지정해야합니다

    template<class account> 
    inline void accountStream::pushToStore(account c) 
    { 
        accountStore.push_back(c); 
    } 
    
  2. 당신은 포인터 (안 참조에를 받아야합니다 포인터) 여기

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

    호출 기능 :

    new_account(accountStore); 
    
    당신이 변수 (안 포인터)를 선언 할 수 있습니다, 또는

    new_account(accountStore); 
    

    :

    당신은 매개 변수로 포인터로 함수를 호출해야합니다

    및 참조를 받으십시오 :

    void new_account(accountStream &accountStorage) 
    { 
        newAccount *account = new newAccount; 
        accountStorage.pushToStore(account); 
    } 
    
  4. 함수가 (당신이 포인터에 & 연산자를 사용에서 무엇을 얻을입니다) 포인터에 대한 포인터를 참조를 받아 때문이 아니라명
+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); 
} 

는 유형이고, 당신이 다음 줄에 일을하려고하는 것은와 push_back이 유형 및

물건이 아닙니다. 올바른 코드는 다음과 같습니다 ACCT는 유형 계정의 인스턴스 동안 계정이 유형입니다

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

때문이다.

+1

그래, 사람들이 나 한테 바보 같은 기분이 들었다고 설명한 후에! 내가 할 독서가있어. – Dannys19