1

EDIT #1: Everything before editing the line

<< "Owner: " << (*wo._owner).getLoginName() << endl;

worked completely fine, or at least didn't throw errors on me.

So I have the following code (obviously there is a lot more that if requested I will post, just not sure if more is needed or that is ok):

class Workout
{
private:
    int _workoutid; // the ID of this workout
    User* _owner; // Who did this workout
    float _distance; // in miles
    int _duration; // in seconds
    int _day, _month, _year; // date: MM/DD/YYYY
    int _weight; // lb, on the date of workout
    // private methods (if necessary)
public:
    friend std::ostream& operator<< (ostream& out, Workout& wo)
    {
        out << "Workout ID: " << wo._workoutid << endl
            << "Owner: " << (*wo._owner).getLoginName() << endl
            << "Distance: " << wo._distance << endl
            << "Duration: " << wo._duration / 3600 << ":" << (wo._duration % 3600) / 60 << ":" << wo._duration % 60 << endl
            << "Date: " << wo._month << ":" << wo._day << ":" << wo._year << endl
            << "Weight: " << wo._weight << endl;
        return out;
    }
    // Print workout id, owner’s username, distance
    // duration (HH:MM:SS), date (MM:DD:YY) and weight of
    // the workout
    // and other public methods (mutators/setters, accessors/getters)
    Workout(void);
    Workout(int, User*, float, int, int, int, int, int);
    virtual ~Workout(void);
    float getDistance();
    void setDistance(float);
};
Workout::Workout(void) : _workoutid(), _distance(), _duration(), _day(), _month(), _year(), _weight()
{
    _owner = new User();
}
Workout::Workout(int id, User* user, float distance, int duration, int day, int month, int year, int weight) :
_workoutid(id), _distance(distance), _duration(duration), _day(day), _month(month), _year(year), _weight (weight), _owner(user)
{
}
Workout::~Workout(void)
{
    delete [] _owner;
}

class User
{
private:
    char* _firstname; // First name
    char* _lastname; // Last name
    char* _loginname; // Login name
    char* _password; // password
    Workout* _myWorkouts[50];// an array of pointers to workouts
    int _numWorkouts; // Num. of workout logged
    User* _buddies[10]; // Friends
    int _numBuddies; // Num. of friends

    // private methods (if necessary)

public:
    friend std::ostream& operator<< (ostream& out, User& user)
    {
        out << "First Name: [" << user._firstname << "]" << endl
            << "Last Name: ["<< user._lastname << "]" << endl
            << "Login Name: [" << user._loginname << "]" << endl
            << "Number of Workouts: [" << user._numWorkouts << "]" << endl
            << "Number of Friends: [" << user._numBuddies << "]" << endl;
        return out;
    }
    User(void);
    User(const char*, const char*, const char*, const char*);
    virtual ~User(void);

    char* getPassword(void);
    char* getLoginName(void);
    char* getFirstName(void);
    void addWorkout(Workout*);
    Workout* getWorkout(int);
    void addBuddy(User* buddy);
    // and other public methods (mutators/setters, accessors/getters)
};
User::User(void) : _firstname(), _lastname(), _loginname(), _password(),
    _myWorkouts(), _numWorkouts(), _buddies(), _numBuddies()
{
}
User::User(const char* first, const char* last, const char* login, const char* pass) : _myWorkouts(), _numWorkouts(), _buddies(), _numBuddies()
{
    _firstname = new char[20];
    _lastname = new char[20];
    _loginname = new char[20];
    _password = new char[20];

    for (int i=0; i < 20; i++){
        _firstname[i] = first[i];
        _lastname[i] = last[i];
        _loginname[i] = login[i];
        _password[i] = pass[i];
    }
}
User::~User(void)
{
    delete [] _firstname;
    delete [] _lastname;
    delete [] _loginname;
    delete [] _password;

    for(int i=0;i<50;i++) delete _myWorkouts[i];
    delete [] _myWorkouts;

    for(int i=0;i<10;i++) delete _buddies[i];
    delete [] _buddies;

    //What about variables such as _numWorkouts and _numBuddies?
}

And I am getting the following errors:

Error 1 error C2027: use of undefined type 'User'

Error 2 error C2228: left of '.getLoginName' must have class/struct/union

The first error is because the operator<< method somehow doesn't want to recognize that the (*wo._owner) object of type User initialized (which it is!) The second error, obviously must be related to the second one, but it doesn't improve my chances of realizing at all how to solve the problem.

Community
  • 1
  • 1
Ren
  • 4,594
  • 9
  • 33
  • 61
  • It may be of assistance in providing an answer if you could post the class(?) definition of User as well. – enhzflep Sep 11 '12 at 04:36
  • done, sorry it took more than 1 minute. – Ren Sep 11 '12 at 04:44
  • 1
    Sorry if you have this covered, it's not obvious from your question. I notice that you have the function declaration inside the class definitions. When I've seen this in the past, it's usually indicative of all the code being kept in the same file. In either case - single file or multiple files, you need to have User defined _before_ Workout. The code shown above could never work as is, since the Workout definition appears before the User definition. – enhzflep Sep 11 '12 at 04:52
  • OMG! You worked magic! LOL. Yes, I have made an effort to keep the classes in due order since my professor wants this project in a single file. It's something of a pain. Thank you very much! – Ren Sep 11 '12 at 04:57
  • Off-topic but, if you really want your classes to manage memory themselves with `new` and `delete`, make sure you understand the [Rule of Three](http://stackoverflow.com/questions/4172722). Alternatively, let `std::string` and `std::vector` take care of it for you. – Mike Seymour Sep 11 '12 at 06:28
  • Yeah, I didn't know the Rule of Three was called like that, but I always kept in mind. Also, unfortunately I can't use any other library aside from ostream :( blame it on the professor. – Ren Sep 11 '12 at 14:52

1 Answers1

3

If this is indeed the structure of your code then you're trying to use "User" before it's defined.

You can't do this.

Declare your output operator as friend if need be and define it after the definition of User is known.

Edward Strange
  • 40,307
  • 7
  • 73
  • 125
  • you mean like taking the definition of `ostream& operator<< ()` out of the class definition and put it like the constructors are defined? – Ren Sep 11 '12 at 04:53