0

I receive this error when trying to compile a derived class. This my first time working with inherited classes so I am not sure what is wrong.

(edit: I have been working on the project and changed it somewhat. Also, here are the error messages from my compiler. Here is the entire thing if it helps https://repl.it/LYwt/8)

#ifndef SHOPPINGCART_H
#define SHOPPINGCART_H

#include <iostream>
#include <iomanip>
#include "Bag.h"
#include "item.h"

template<class ItemType>
class shoppingcart : public Bag<ItemType>
{

private:
  float totalPrice;
public:
  shoppingcart();
  double getTotalPrice();
  bool add(item);
  bool remove(item);
};  

#endif

header file:

#include "shoppingcart.h"
using namespace std;

template <class ItemType>
shoppingcart<ItemType>::shoppingcart() : totalPrice(0.00) 
{ 
}


template <class ItemType>
bool shoppingcart<ItemType>::add(const item newProduct) 
{

  bool added = Bag<ItemType>::add(newProduct);
  totalPrice = totalPrice + (newProduct.getitemQuantity() * newProduct.getitemPrice());

  return added;
}

template <class ItemType>
bool shoppingcart<ItemType>::remove(const item aProduct) 
{

  bool removed = Bag<ItemType>::remove(aProduct);
  totalPrice = totalPrice - (aProduct.getitemQuantity() * aProduct.getitemPrice());

return removed;
}   
  float price;
  template <class ItemType>
  double shoppingcart<ItemType>::getTotalPrice()
  return totalPrice;
}

error messages:

shoppingcart2.cpp:5:1: error: 'shoppingcart' does not name a type shoppingcart::shoppingcart() : totalPrice(0.00) ^~~~~~~~~~~~

shoppingcart2.cpp:11:18: error: expected initializer before '<' token bool shoppingcart::add(const item newProduct) ^

shoppingcart2.cpp:21:18: error: expected initializer before '<' token bool shoppingcart::remove(const item aProduct) ^

shoppingcart2.cpp:31:20: error: expected initializer before '<' token double shoppingcart::getTotalPrice()

Caschmitt
  • 3
  • 2

1 Answers1

0

Is this what you are looking for?

#include <iostream>
#include <string>

template<class ItemType>
class Bag
{};

template<class ItemType>
class shoppingCart : public Bag<ItemType>
{

private:
    double totalPrice;
public:
    shoppingCart<ItemType>(){ }
    double calculateTotal(const ItemType& item_price, const ItemType& item_quantity);
};  

template<class ItemType>
double shoppingCart<ItemType>::calculateTotal(const ItemType& item_price, const ItemType& item_quantity)
{
    totalPrice = getitemPrice() * getitemQuantity();
}

struct an_item
{
    double cost;
    std::string name;
};

int main( )
{
    shoppingCart<an_item> cart;
    return 0;
}

When exploring code, do it all in one cpp file, so much easier to write and share. The biggy was item& and you had no declaration for it. I'll assume you really wanted to use ItemType& for your references. But it doesn't make a lot of sense to me as you refer to it as both a price and quantity. So if ItemType is a class you will have to specialize it. Or do you intend it to be a POD? I really helps if you use one file for your code and post the whole thing.

lakeweb
  • 1,859
  • 2
  • 16
  • 21