Feb 19, 2016 at 10:44pm UTC
Hi,
I'm trying to access the vector foodOrder from customerOrder (via printorder) by declaring it a friend but it's not working?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
class food : virtual public someOtherClass {
friend class customerOrder;
public :
food();
//copy constructor
food(const food& foods);
~food();
...
private :
...
std::vector <food*> foodOrder;
};
class customerOrder {
public :
customerOrder();
~customerOrder();
void printOrder(); // get a "foodOrder was not declared in this scope" error
private :
;
};
thanks
Last edited on Feb 19, 2016 at 10:49pm UTC
Feb 19, 2016 at 11:02pm UTC
To access a non-static member of the class you need to have an instance of the class. I mean, you can't just magically access foodOrder from within printOrder() because which food object's foodOrder is it that you want?
Feb 19, 2016 at 11:25pm UTC
Oke so I added a food object in customerOrder in private as well:
food neworder;
and used it in printorder so:
newOrder.foodOrder[i]->foodName ;
does that look oke? I'm not getting errors anymore about that , but now this:
Now I'm getting some other error as well :
template<typename _CharT, typename _Traits, typename _Alloc>
inline bool
operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
const _CharT* __rhs)
{ return __lhs.compare(__rhs) == 0; } //it complained here.
do I need to create a copy constructor or such for this last error?
thanks :)
Feb 20, 2016 at 1:39am UTC
can anyone help , I'm very confused by the error I'm getting.
Feb 20, 2016 at 1:51am UTC
here's my code now, or parts of it
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
...
class food : virtual public otherclass {
friend class customerOrder;
public :
food();
~food();
void addItem();
void setPrice();
bool operator ==(const std::string foodString);
...
private :
food* foodItem;
...
std::vector <food*> foodOrder;
};
class customerOrder {
public :
customerOrder();
~customerOrder();
void printOrder();
private :
food newOrder;
};
food::~food(){
foodItem = 0;
}
...
void food::addItem(){
int count = 0;
std::cout << std::endl;
std::cout << "Enter the food item from chosen " << userChoice << " menu :" << std::endl;
bool done = false ;
while (done == false ) {
foodItem = new food;
userChoice = "?" ;
std::cin >> userChoice;
foodItem->foodName = userChoice;
std::cout << "How many of these ?... " << std::endl;
std::cin >> count;
while (count != 0) {
foodOrder.push_back(foodItem);
--count;
}
delete foodItem;
std::cout << "Would you like to order more items? Yes or No ?" ;
std::cin >> userChoice;
if ((userChoice == "no" ) || (userChoice == "No" ) || (userChoice == "nO" ) || (userChoice == "NO" )){
done = true ;
} else {
std::cout << "Enter the next food item..." << std::endl;
}
}
}
bool food::operator ==(const std::string foodString) {
return ( this ->foodName == foodString);
}
void food::setPrice(){
float priceAr[] = {1.00, 1.50, 2.00, 3.00, 4.00, 5.00, 6.00, 7.00, 8.00};
for (size_t i = 0; i < foodOrder.size();++i) {
std::transform(foodName.begin(), foodName.end(),foodName.begin(), ::toupper);
if (foodOrder.at(i)->foodName == "PIZZA" ){ //the problem is here
// and i get the above error .
}
...
thanks
Last edited on Feb 20, 2016 at 1:53am UTC