-4

Here's my relevant portion of the .hpp file:

ifndef CUSTOMER_HPP
#define CUSTOMER_HPP

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

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

Here's my relevant portion of the .cpp file:

#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
}

The full error is:

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> >&’

If I remove the line cart = new vector; I get a segmentation fault when I try to perform:

cart.push_back(random_string);

I suspect because it hasn't been initialized. So I added in the above line, and now have the above error.

cart.push_back(random_string) is called in:

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

The argument that's passed into cart.push_back() is from

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

And the getMemberFromID() function that's called there is:

 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;                                                                                                         
}    
J. Siek
  • 43
  • 2
  • 10
  • Why are you using new? `cart` is already a vector. That line is wrong, and if you're having a crash you should show the actual code that is crashing. – Retired Ninja Nov 02 '16 at 03:59
  • I guess you're coming from Java background, you can read these books first to understand how C++ work http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Danh Nov 02 '16 at 04:01
  • When I try to do cart.push_back(random_string), I get a segmentation fault. I thought that this was because the vector wasn't initialized, so I tried to initialize it in the constructor. – J. Siek Nov 02 '16 at 04:05
  • Show us a short complete example of the code that crashes. If I had to guess I'd say you have a null pointer to your Customer object and that's why it's crashing when you try and access a member. – Retired Ninja Nov 02 '16 at 04:14
  • `void Customer::addProductToCart(string st){ cout << "adding " << st << " to cart." << endl; getCart().push_back(st); }` – J. Siek Nov 02 '16 at 04:15
  • You should show your `GetCart` implementation. Given that its within the class is it really required to use a getter function? Again screams java. Is `GetCart` returning a reference? – Paul Rooney Nov 02 '16 at 04:17
  • Out of context that code might be fine, although why you're using a getter for an internal member isn't clear and since we can't see what that function does it's not helpful. In context the issue will probably be very clear. – Retired Ninja Nov 02 '16 at 04:17
  • Alright, I updated my original post. getCart() returns cart. I originally just called cart.push_back, but when I started getting segmentation faults I switched it around to see if it would change. – J. Siek Nov 02 '16 at 04:24
  • 1
    Think about what happens when `getMemberFromID(mID)` returns null. Does `getCart()` return a reference or a copy? – Retired Ninja Nov 02 '16 at 04:28
  • Huh, you guys are onto something. getMemberFromID returns null. I put in some debug messages into getMemberFromID to find out why and got this for the following line: `cout << "Comparing " << *(members.at(i)) << " to " << id << endl;` `Comparing 0x13b4300 to 0000 Comparing 0x13b43b0 to 0000 Comparing 0x13b4460 to 0000` So I guess I'm comparing a memory address to a string? How exactly do I fix that? – J. Siek Nov 02 '16 at 04:33
  • Wait, I'm retarded. Never mind. – J. Siek Nov 02 '16 at 04:40

1 Answers1

0

You're not using pointers, so this line:

cart = new vector<string>;

is wrong. You don't need to initialize it.

Ryan
  • 14,392
  • 8
  • 62
  • 102
  • I think you're right. I removed that line, but I still get a segmentation fault when I try to do cart.push_back(random_string). – J. Siek Nov 02 '16 at 04:12