編集 #1: 行を編集する前のすべて
<< "所有者: " << (*wo._owner).getLoginName() << endl;
完全に正常に機能したか、少なくともエラーは発生しませんでした。
だから私は次のコードを持っています(明らかに、要求された場合に投稿することはもっとたくさんありますが、もっと必要かどうか、それで問題ないかどうかはわかりません):
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?
}
そして、次のエラーが発生します。
エラー 1 エラー C2027: 未定義の型 'User' の使用
エラー 2 エラー C2228: '.getLoginName' の左側には、class/struct/union が必要です
最初のエラーは、operator<< メソッドが User 型の (*wo._owner) オブジェクトが初期化されていることを何らかの形で認識したくないためです (これはそうです!) 2 番目のエラーは、明らかに 2 番目のエラーに関連している必要がありますが、問題を解決する方法を理解する可能性はまったく向上しません。