获得“未定义类型的使用".和“必须具有class/struct/union"错误 [英] Getting "Use of undefined type" and "Must have class/struct/union" errors

查看:89
本文介绍了获得“未定义类型的使用".和“必须具有class/struct/union"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑#1:编辑该行之前的所有内容

EDIT #1: Everything before editing the line

<< 所有者:"<< (* wo._owner).getLoginName()<< endl;

<< "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?
}

我收到以下错误:

错误1错误C2027:使用未定义的类型'用户'

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

错误2错误C2228:.getLoginName"的左侧必须具有class/struct/union

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

第一个错误是因为运算符<<方法以某种方式不想识别用户类型为(* wo._owner)的对象已初始化(它是!) 第二个错误,显然必须与第二个错误有关,但这并没有增加我完全意识到如何解决问题的机会.

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.

推荐答案

如果这确实是代码的结构,那么您将在定义"User"之前尝试使用它.

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

您不能这样做.

在需要时将您的输出运算符声明为好友,并在知道User的定义后对其进行定义.

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

这篇关于获得“未定义类型的使用".和“必须具有class/struct/union"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆