2016-11-02 12 views
-4

の引数1の変換はわかりません。これは私の関連する.hppファイルの部分です:'std :: vector <std :: basic_string <char>> *'から 'const std :: vector <std :: basic_string <char>>&'

ifndef CUSTOMER_HPP 
#define CUSTOMER_HPP 

#include <vector> 
#include "Product.hpp" 

class Customer 
{ 
private: 
    std::vector<std::string> cart; 
    //other random member variables 

ここで.cppファイルの私の関連する部分です:

#include "Customer.hpp" 
#include "Product.hpp" 
#include <iostream> 
using namespace std; 

Customer::Customer(string n, string a, bool pm){ 
    cart = new vector<string>; 
    //other random member variables 
} 

完全なエラーがある:

Customer.cpp:10:7: note: candidate is: 
In file included from /usr/include/c++/4.8/vector:69:0, 
      from Customer.hpp:4, 
      from Customer.cpp:1: 
/usr/include/c++/4.8/bits/vector.tcc:160:5: note: std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(const std::vector<_Tp, _Alloc>&) [with _Tp = std::basic_string<char>; _Alloc = std::allocator<std::basic_string<char> >] 
vector<_Tp, _Alloc>:: 
^ 
/usr/include/c++/4.8/bits/vector.tcc:160:5: note: no known conversion for argument 1 from ‘std::vector<std::basic_string<char> >*’ to ‘const std::vector<std::basic_string<char> >&’ 

私は行を削除する場合 カート=新しいベクトル; 私が実行しようとしたとき、私はセグメンテーションフォールトを取得:

cart.push_back(random_string); 

それが初期化されていないので、私は疑います。だから私は上記の行に追加し、今は上記のエラーがあります。

cart.push_back(random_stringが)に呼び出されます。

void Customer::addProductToCart(string st){ 
    cout << "adding " << st << " to cart." << endl; 
    getCart().push_back(st); 
} 

cart.push_backに渡された引数は、()

void Store::addProductToMemberCart(string pID, string mID){ 
    cout << "HIT in addProductToMemberCarti. pID = " << pID << ", mID = " << mID << endl; 
    getMemberFromID(mID)->addProductToCart(pID);                   } 

からのものであり、そこに呼ばれていますgetMemberFromID()関数があります:

Customer* Store::getMemberFromID(string id){                     

    for(int i = 0; i < members.size(); i++){                   
     if(members.at(i)->getAccountID() == id){                 
      return members.at(i);                       
     }                             
    }                              
    return NULL;                           
}  
+0

なぜ新しいものを使用していますか? 'cart'はすでにベクトルです。その行は間違っています。クラッシュしている場合は、クラッシュしている実際のコードを表示する必要があります。 –

+0

私はあなたがJavaのバックグラウンドから来ていると思いますが、まずC++の動作を理解するためにこれらの書籍を読むことができますhttp://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Danh

+0

cart.push_back(random_string)を実行しようとすると、セグメンテーション違反が発生します。これはベクトルが初期化されていないためだと思ったので、コンストラクタで初期化しようとしました。 –

答えて

0

あなたはポインタを使用していないので、この行は

です
cart = new vector<string>; 

が間違っています。初期化する必要はありません。

+0

私はあなたが正しいと思います。私はその行を削除しましたが、cart.push_back(random_string)を実行しようとするとまだセグメンテーション違反が発生します。 –

関連する問題