fixing undefined reference

closed account (GL1Rko23)
The undefined reference is on line 33 in "customer.cpp" I had the method "amountFor" originally in customer.cpp but I want to move it to rental.cpp while still calling it in customer.cpp. How do I fix moving this method?


Rental.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "rental.h"


void Rental::amountFor(Rental & aRental, double & thisAmount)
{
    switch (aRental.getMovie().getPriceCode()){
        case Movie::REGULAR:
            thisAmount += 2;
            if(aRental.getDaysRented() > 2)
                thisAmount += (aRental.getDaysRented() - 2) * 1.5;

            break;
        case Movie::NEW_RELEASE:
            thisAmount += aRental.getDaysRented() * 3;
            break;
        case Movie::CHILDRENS:
            thisAmount += 1.5;
            if(aRental.getDaysRented() > 3)
                thisAmount += (aRental.getDaysRented() - 3) * 1.5;

            break;
    }
}


Rental.h

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
#ifndef RENTAL_H
#define RENTAL_H

#include "movie.h"

class Rental
{
  public:
    // Comstructors
    Rental(Movie movie, int days_rented)
      : _movie(movie), _days_rented(days_rented)
    { }

    // Accessors
    int   getDaysRented() { return _days_rented; }
    Movie getMovie()      { return _movie;       }

    // Mutators

  private:
    Movie _movie;
    int   _days_rented;
    void amountFor(Rental & aRental, double & thisAmount);

};

#endif 


customer.cpp

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
/*  Customer implementation
 */

#include "customer.h"
#include "movie.h"

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using std::cout;
using std::istringstream;
using std::string;
using std::vector;



string Customer::statement()
{
  double totalAmount          = 0;
  int    frequentRenterPoints = 0;

  // Init result string
  string result("Rental Record for ");
  result += getName();
  result += "\n";

  for (vector<Rental>::iterator it = _rentals.begin();
                                it != _rentals.end(); ++it)
  {
      double thisAmount = 0;
      Rental& each = *it;
      amountFor(each, thisAmount);
      // Add frequent renter's points
        ++frequentRenterPoints;

        // Add bonus for new release rental
        if ( each.getMovie().getPriceCode() == Movie::NEW_RELEASE &&
             each.getDaysRented() > 1 )
            ++frequentRenterPoints;

        // Include in result
        result += "\t";
        result += each.getMovie().getTitle();
        result += "\t";

        // Convert int to a string.
        // Output the int object into the output string stream.
        std::ostringstream out_str_stream;
        out_str_stream << thisAmount;
        result += out_str_stream.str();
        result += "\n";
        totalAmount += thisAmount;
    }
    result += "Amount owed is: ";

     // Convert int to a string.
     // Output the int object into the output string stream.
    std::ostringstream out_str_stream;
    out_str_stream << totalAmount;
    result += out_str_stream.str();
    result += "\n";
    result += "You earned: ";
    std::ostringstream out_str_stream2;
    out_str_stream2 << frequentRenterPoints;
    result += out_str_stream2.str();
    result += " frequent renter points\n";

    return result;
}





customer.h

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
#ifndef CUSTOMER_H
#define CUSTOMER_H

#include "rental.h"

#include <string>
#include <vector>

class Customer
{
  public:
    // Constructors
    Customer(std::string name)
      : _name(name)
    { }

    // Accessors
    std::string getName() { return _name; }

    // Mutators
    void addRental(Rental rental)
      { _rentals.push_back(rental); }

   // Other
   std::string statement();

  private:
    std::string         _name;
    std::vector<Rental> _rentals;
    void amountFor(Rental & aRental, double & thisAmount);

};

#endif 
amountFor is a class function of the Customer class, and the Rental class.

You call a class method on an instance of a class

e.g.

1
2
Rental someRentalObject;
someRentalObject.amountFor(anotherRentlaObject. 20.0);


You cannot call class functions as if they were not class functions.
Topic archived. No new replies allowed.