Referencing a class within a different class using a vector
Jun 8, 2018 at 10:17pm UTC
So I have this class "Shopping Card" referencing a class I made named "ItemToShop" using a vector. This is coming up 'ItemToPurchase': undeclared identifier. I'm not sure what is going wrong, but I think it is because of how I am incorporating the file.
1 2 3 4 5 6 7 8 9 10 11 12 13
//ShoppingCart.h
#pragma once
class ShoppingCart
{
public :
ShoppingCart(string customerNameIn, string dateIn);
private :
string customerName = "none" ;
string date = "January 1, 2016" ;
vector<ItemToPurchase> itemVector;
};
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//ShoppingCart.cpp
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <iomanip>
using namespace std;
#include "ShoppingCart.h"
#include "ItemToPurchase.h"
ShoppingCart::ShoppingCart(string customerNameIn, string dateIn)
{
customerName = customerNameIn;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
//ItemToPurchase.h
#pragma once
class ItemToPurchase
{
private :
string itemName;
double itemPrice;
int itemQuantity;
string description;
public :
ItemToPurchase();
void SetName(string nameIn);
string GetName();
void SetPrice(double priceIn);
double GetPrice();
void SetQuantity(int quantityIn);
int GetQuantity();
void SetDescription(string descriptionIn);
string GetDescription();
string PrintCost();
string PrintDescription();
};
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
//ItemToPurchase.cpp
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
using namespace std;
#include "ItemToPurchase.h"
ItemToPurchase::ItemToPurchase()
{
itemName = "" ;
itemPrice = 0.0;
itemQuantity = 0;
}
void ItemToPurchase::SetName(string nameIn)
{
itemName = nameIn;
}
string ItemToPurchase::GetName()
{
return itemName;
}
void ItemToPurchase::SetPrice(double priceIn)
{
itemPrice = priceIn;
}
double ItemToPurchase::GetPrice()
{
return itemPrice;
}
void ItemToPurchase::SetQuantity(int quantityIn)
{
itemQuantity = quantityIn;
}
int ItemToPurchase::GetQuantity()
{
return itemQuantity;
}
void ItemToPurchase::SetDescription(string descriptionIn)
{
description = descriptionIn;
}
string ItemToPurchase::GetDescription()
{
return description;
}
string ItemToPurchase::PrintCost()
{
ostringstream costStream;
const int MONEY_PRECISION = 2;
costStream << itemName << " " << itemQuantity << " @ $" << fixed << setprecision(MONEY_PRECISION) << itemPrice << " = $" << itemQuantity * itemPrice;
//Ex: Bottled Water 10 @ $1.50 = $15.00
return costStream.str();
}
string ItemToPurchase::PrintDescription()
{
ostringstream descriptionStream;
descriptionStream << itemName << ": " << description;
//Ex: Bottled Water: Deer Park, 12 oz.
return descriptionStream.str();
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
//main.cpp
#include <iostream>
#include <cctype>
#include <ios>
#include <iomanip>
#include <string>
#include <math.h>
#include <vector>
#include <fstream>
#include <limits>
#include <sstream>
using namespace std;
#include "ItemToPurchase.h"
#include "ShoppingCart.h"
int main()
{
//code
}
Jun 8, 2018 at 10:48pm UTC
First file:
1 2 3 4 5 6 7 8
//ShoppingCart.h
#pragma once
class ItemToPurchase; // forward declare a typename
class ShoppingCart
{
// ...
Jun 8, 2018 at 11:21pm UTC
I just had to include ItemToPurchase in my ShoppingCart.h
Thanks!
Jun 9, 2018 at 11:49am UTC
There is no reason to include the whole definition, when forward declaration is sufficient.
Topic archived. No new replies allowed.