[try Beta version]
Not logged in

 
Passing Class Object by ref help

May 22, 2015 at 11:30pm
I got everything working in my program except I get an error when passing my object by reference:

//main.cpp|50|undefined reference to `showOrder(Sandwhich)'|

Here is my code (sorry, it is a tad bit long). Spoilers also aren't working for some reason

Main CPP
[spoiler]
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
#include <iostream>
#include <string>

#include "Sandwhich.h"

using namespace std;

const int ROWS = 10, COLS = 40;
char MENU[ROWS][COLS] = {
"#######################################",
"#               M E N U               #",
"# BREAD:   MEAT:   CHEESE:   DRINK:   #",
"# white    ham     swiss     soda     #",
"# wheat    turkey  cheddar   water    #",
"# flat     salami  jack      ice tea  #",
"#          steak                      #",
"#                                     #",
"#     Thanks for choosing Hoagie's!   #",
"#######################################",
};

void displayMenu(char menu[ROWS][COLS]);
void showOrder(Sandwhich order);

int main()
{
    string bread, cheese, meat, drink;
    int orders;
    double price = 0;

    displayMenu(MENU);

    cout << "What type of bread do you want? ";
    getline(cin, bread);

    cout << endl << "Cheese? ";
    getline(cin, cheese);

    cout << endl << "Meat? ";
    getline (cin, meat);

    cout << endl << "Drink? ";
    getline(cin, drink);

    cout << endl << "How many orders? ";
    cin >> orders;

    Sandwhich Order_1(bread, cheese, meat, drink, orders, price);
    Order_1.calculatePrice();
    showOrder(Order_1);
    return 0;
}

void displayMenu(char menu[ROWS][COLS]) {
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++)
        {
            cout << menu[i][j];
        }
        cout << endl;
    }
    cout << endl;
}

void showOrder (Sandwhich& order) {
    cout << "Your Order: ";
    cout << order.getBread() + ", " + order.getMeat() + ", " +
            order.getCheese() + ", " + order.getDrink()
    << endl;
    cout << "Your Price: " << order.getPrice() << endl;
}

[/spoiler]

Sandwhich header
[spoiler]
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
#ifndef SANDWHICH_H
#define SANDWHICH_H

#include <iostream>
#include <string>

using namespace std;

class Sandwhich
{
    public:
        //Default Constructor
        Sandwhich();

        //Overload Constructor
        Sandwhich(string, string, string, string, int, double);

        //Destructor
        virtual ~Sandwhich();

        //Accessor Methods
        string getBread() const;

        string getCheese() const;

        string getMeat() const;

        string getDrink() const;

        int getOrders() const;

        double getPrice() const;

        //Mutator Methods
        double calculatePrice();

    protected:
    private:
        //Member Variables
        string newBread, newCheese, newMeat, newDrink;
        int newOrders;
        double newPrice;
};

#endif //SANDWHICH_H

[/spoiler]

Sandwhich class CPP
[spoiler]
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
74
75
#include "Sandwhich.h"

Sandwhich::Sandwhich() //constructor
{
    //strings are automatically null
    newOrders = 0;
    newPrice = 0.0;
}

Sandwhich::Sandwhich (string bread, string cheese, string meat, string drink, int orders, double price) {
    newBread = bread;
    newCheese = cheese;
    newMeat = meat;
    newDrink = drink;
    newOrders = orders;
    newPrice = price;
}

Sandwhich::~Sandwhich()
{
    //dtor
}

string Sandwhich::getBread() const {
    return newBread;
}

string Sandwhich::getCheese() const {
    return newCheese;
}

string Sandwhich::getMeat() const {
    return newMeat;
}

string Sandwhich::getDrink() const {
    return newDrink;
}

int Sandwhich::getOrders() const {
    return newOrders;
}

double Sandwhich::getPrice() const {
    return newPrice;
}

double Sandwhich::calculatePrice() {
        //Bread price
    if (getBread() == "white" || getBread() == "wheat")
        newPrice = newPrice + 2.00;
    else if (getBread() == "flat")
        newPrice = newPrice + 2.50;

        //Cheese price
    if (getCheese() == "swiss" || getCheese() == "cheddar")
        newPrice = newPrice + 1.00;
    else if (getCheese() == "jack")
        newPrice = newPrice + 1.50;

        //Meat price
    if (getMeat() == "ham" || getMeat() == "turkey")
        newPrice = newPrice + 2.50;
    else if (getMeat() == "salami")
        newPrice = newPrice + 3.00;
    else if  (getMeat() == "steak")
        newPrice = newPrice + 3.50;

        //Drinks price
    if (getDrink() == "soda" || getDrink() == "ice tea")
        newPrice = newPrice + 1.00;

        return newPrice * getOrders();
}

[/spoiler]

Thanks to all that reply, and yes, I realize I have spelled sandwich incorrectly lol
Last edited on May 22, 2015 at 11:31pm
May 22, 2015 at 11:45pm
Line 23 of the first block of code. You forgot an ampersand.
May 22, 2015 at 11:53pm
Line 23 of the first block of code. You forgot an ampersand.

Gah, stupid error. Thanks
Topic archived. No new replies allowed.