-2

In my code, I want to create a cookie and add it to a shop by sending the cookie to shop constructor as parameter. It adds the cookie but give segmentation fault error.

I get the result:

  1. Chocolate Cookie 50 180
    Segmentation fault (core dumped)!

I cannot find the code part where I am wrong with. Can you help?

main.cpp:

#include <iostream>
#include <string>
#include "Shop.h"
#include "Cookie.h"
using namespace std;


int main(){
    Cookie cookie1("Chocolate Cookie", 50, 180);
    Cookie cookie2("Cake Mix Cookie", 60, 200);

    Shop<Cookie> cookieShop(cookie1);
    //cookieShop.Add(cookie2);
    cout << cookieShop ;

    return 0;
}

Shop.h:

#ifndef SHOP_T
#define SHOP_T
#include <string>
using namespace std;

template<class type>
class Shop;

template<typename type>
ostream& operator<<(ostream& out, const Shop<type>& S){
    for(int i = 0; i < S.size; i++)
        out << i + 1 << ".\t" << S.list[i] << endl;
}

template<class type>
class Shop{
    type *list;
    int size;
public:
    Shop() { list = 0; size = 0; }
    Shop(type t);
    ~Shop(){ delete[] list; }
    void Add(type A);
    friend ostream& operator<< <>(ostream& out, const Shop<type>& S);
};

template<class type>
Shop<type>::Shop(type t) : size(0){
    list = new type();
    list[0] = t;
    size++;
}

template<class type>
void Shop<type>::Add(type A){
    type *temp = new type[size+1];
    for(int i = 0; i < size; i++)
        temp[i] = list[i];
    delete[] list;
    temp[size] = A;
    list = temp;
    size++;
}

#endif

Cookie.h:

#ifndef COOKIE
#define COOKIE
#include <string>
using namespace std;

class Cookie{
    string name;
    int piece;
    float price;
public:
    Cookie(string = "", int = 0, float = 0);
    friend ostream& operator<<(ostream& out, const Cookie& C);
};

#endif

Cookie.cpp:

#include "Cookie.h"
#include <iostream>
#include <string>
using namespace std;

Cookie::Cookie(string n, int pi, float pr){
      name = n;
      piece = pi;
      price = pr;
}

ostream& operator<<(ostream& o, const Cookie& C){
    o << C.name << "\t" << C.piece << "\t" << C.price;
}
Murat
  • 1
  • 3
  • 1
    How did this even compile – Hatted Rooster May 19 '17 at 15:11
  • 2
    Yeah, compiler warnings would have instantly screamed at you about not returning values from functions declared as returning values... – underscore_d May 19 '17 at 15:11
  • @Ðаn: I use Dev-C++. I watched some videos about debugging and tried on my code. But it does not evaluate the shop cotr. I dont know if I am wrong with debugging or compiler cannot do that. I've read that cotrs are not debugged. Is it true? If can, how can I do it. – Murat May 19 '17 at 17:41
  • @Murat If you're getting the segfault as soon as you construct the first `Cookie`, then obviously the problem is there. So, why would you expect to see the debugger reaching the `Shop` constructor, which is called later in your `main()`, when it crashes before that and so can never reach it? Also, please search for information on using stdlib containers instead of raw `new` and `delete`, and using constructor initialisation-lists instead of setting member variables in the ctor body. Oh - and not using `using namespace std;`. – underscore_d May 20 '17 at 12:13
  • when I try to debug, I start from `int main(){ ` line and clicking "next line" until it comes to `return 0;`. Until this time, it prints the `cookie1` and does not give segfault error. if I continue to click "next line", it goes back to the line `Shop cookieShop(cookie1);`, and I click once more on "next line" button, it gives the seg error. I dont understand. – Murat May 20 '17 at 12:52

1 Answers1

3

You forgot to return the ostream in both the operator<< functions. This cause undefined behavior when chaining the << operations.

For example it is equivalent to :

o.operator<<(C.name).operator<<("\t")

Where o.operator<<(C.name) has undefined return value causing the next .operator<< on an invalid reference

Ðаn
  • 10,934
  • 11
  • 59
  • 95
Tezirg
  • 1,629
  • 1
  • 10
  • 20
  • 1
    @Murat If you applied the advice given in the post, and the problem remained (which would kinda surprise me, but OK), then it did not answer your question, so why did you mark it as the answer? – underscore_d May 20 '17 at 12:10