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.