0

I am working on a "Candy Shop" assignment project for OOP with C++ and compiler complains about my code again :) I found a similar named topic in forum but it points to some other issue than I have... So, I got a base class to derive some products ( candy, cookie, vs.) and a template for shopping class as follows:

class productBase{
protected:
    string name;
    double amount;
    double ppAmount;
public:
    productBase(){}
    productBase(string na, double am, double pp){ 
        name = na; 
        amount = am; 
        ppAmount = pp; 
    }    
    string getName() const;
    double getAmount() const;
    double getppAmount() const;
    //virtual double getCost() const = 0;        // make it abstract ?
};

//  Templete for shopping
template <typename shopType>
class Shop {
private:
    int noi;                        //  number of items
    double totalcost;   
    shopType * sTptr;
public:
    Shop();
    Shop(shopType);
    ~Shop() { delete[] sTptr; }

    void add(shopType &);
    void setDiscount(const double);    
    friend ostream& operator<<(ostream&, const Shop<shopType> &);    
    shopType operator[](int);
};

And one of my product class is like this:

class Cookie: public productBase {
private:
    const double taxRate;
public:
    Cookie() :taxRate(8) {}
    Cookie(string nm, double am, double pp) :productBase(nm, am, pp), taxRate(8) {}
    ~Cookie() {}    
    double getCost() const;    
    friend ostream& operator<<(ostream &, Cookie &);    
};

In the program, I need to hold my products in a dynamic array which is first created and expands with adding new instances as follows:

int main() {
.....
    Cookie cookie1("Chocolate Chip Cookies", 10, 180);  
    Cookie cookie2("Cake Mix Cookies", 16, 210);

    Shop<Cookie> cookieShop(cookie1);       // << this is where I am gettin error
    cookieShop.add(cookie2);
.....

and this where I am getting error from the compiler

Error LNK2019 unresolved external symbol "public: __thiscall Shop::Shop(class Cookie)" (??0?$Shop@VCookie@@@@QAE@VCookie@@@Z) referenced in function _main

I thought that it is caused by the constructor of my template and tried to fix it by going over some examples like passing by reference and using base class pointer or even not using a base class at all, but i feel like the problem is something else like a missing parameter or misusage of something but can't figure it out :( So these are the methods of my template class where I am looking for the cause...

template<typename shopType>
Shop<shopType>::Shop()
{
    noi = 0;
    totalcost = 0;
    sTptr = NULL;
}

template<typename shopType> 
Shop<shopType>::Shop(shopType sT)           // I guess the problem is here
{
    sTptr = sT;
    noi++;
    totalcost = sT.getCost();       
}

template<typename shopType>
void Shop<shopType>::add(shopType & toAdd)
{
    if (noi == 0) {
        sTptr = new shopType;
        sTptr = toAdd;
        totalcost = toAdd.getCost();
        noi++;
    }
    else {
        shopType * ptr = new shopType[noi + 1];
        for (int a = 0; a < noi; a++) {
            ptr[a] = sTptr[a];
        }    
        delete[] sTptr;         
        sTptr = ptr;
        sTptr[noi++] = toAdd;  
        totalcost += toAdd.getCost();
    }
}

This template usage is confusing me; I would be already done without using it, but I need to learn it on the other hand :) So what might be I am missing out of sight?

Thanks in advance for any guidance or assistance.

alios
  • 43
  • 9
  • Yes, the methods for templates are in a separate shop.cpp file which includes the shop.h file (#include "shop.h") that has the base class and template class definitions. – alios May 12 '17 at 12:03

1 Answers1

1
template<typename shopType> 
Shop<shopType>::Shop(shopType sT)           // I guess the problem is here
{
    sTptr = sT;
    noi++;
    totalcost = sT.getCost();       
}

You pass an object by value, and then assign it to a pointer to that type. That's not valid for any type. The logic here should be similar to add.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
  • and the error complains about unresolved symbol because the constructor as written is invalid and thus not provided for that specific instantiation? Well it isnt valid for any instantiation unless the type `shopTpye` offers some object to pointer conversion, but otherwise I cannot explain the error... – 463035818_is_not_an_ai May 12 '17 at 12:06
  • @tobi303 - I didn't read the error closely enough. The duplicate Nathan linked has the solution. I just left this here so the OP knows what's wrong when it fails again. You can down-vote the hell out of it, I know it doesn't answer the question :) – StoryTeller - Unslander Monica May 12 '17 at 12:08
  • I was suspicious about that and tried to implement it like that but the issue @NathanOliver pointed was ruining the whole compiling process i guess.. But you are right also I need to face the problem there after I fix removing the declarations to header file :) – alios May 12 '17 at 12:20