这是.hpp文件的相关部分:
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 = new vector; 我尝试执行时遇到分段错误:
cart.push_back(random_string);
我怀疑是因为它尚未初始化。所以我在上面的行中添加了,现在出现了上述错误。
在。:/ / p>中调用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 :(得分:0)
你没有使用指针,所以这一行:
cart = new vector<string>;
错了。您无需初始化它。