方法调用 returns 空值

Method call returns null value

以下代码可以正常工作。但是,在调用 getName() 方法时,在 UserDB class 方法 adduser(AccountInfo* newUser) 中,返回空值。我确定指针存在问题,但代码在审查后似乎没问题。方法调用不正确?

#include <iostream>
    using namespace std;
    class AccountInfo
    {
    private:
    char* _userLoginName; // store the login name of the user
    unsigned int _uid; //user identifier

public:
    AccountInfo(); // empty constructor
    AccountInfo(char* line); // constructor
    ~AccountInfo(); // destructor

    // other public methods (mutators/setters, accessors/getters)
    char* getName();
    unsigned int getUID();
    void setName(char* name1);
    void setUID(unsigned int UID1);
};
AccountInfo::AccountInfo(){

}
AccountInfo::AccountInfo(char* line){
    _line = line;
    char bufferLine[256];
    char tempNumber[4];
    char tempName[20];
    unsigned int length1 = 0;
    unsigned int tempLength1 = 0;

    //find length of line
    while (_line[length1] != '[=11=]'){
        length1++;
    }
    //separate the text by white space
    for (int i = 8; i < length1 + 1; i++){
        bufferLine[tempLength1 + 1] = '[=11=]';

        if ((_line[i] == ' ') || (_line[i] == '[=11=]'))
        {

            if (bufferLine[0] == '-')
            {

                if (bufferLine[1] == 'u'){
                    bufferLine[0] = '[=11=]';
                    tempLength1 = 0;
                    i++;
                    while (_line[i] != '-'){
                        bufferLine[tempLength1] = _line[i];
                        i++;
                        tempLength1++;
                    }
                    bufferLine[tempLength1] = '[=11=]';
                    printf("\n");
                    printf(bufferLine);
                    printf("\nis UID \n");
                    i--;

                    tempNumber[0] = bufferLine[0];
                    tempNumber[1] = bufferLine[1];
                    tempNumber[2] = bufferLine[2];
                    tempNumber[3] = bufferLine[3];

                    _uid = ((tempNumber[0] - '0') * 1000) + ((tempNumber[1] - '0') * 100) + ((tempNumber[2] - '0') * 10) + (tempNumber[3] - '0');
                    setUID(_uid);
                }
            }
            else{
                //is name
                bufferLine[tempLength1] = '[=11=]';
                setName(bufferLine);
            }
            bufferLine[0] = '[=11=]'; //reset buffer line
            tempLength1 = 0; // reset incrementation for buffer line
        }
        else{
            bufferLine[tempLength1] = _line[i];
            tempLength1++;
        }
    }
}
AccountInfo::~AccountInfo(){
    delete[] _userLoginName;

}
char* AccountInfo::getName(){
    return  _userLoginName;
}

unsigned int AccountInfo::getUID(){
    return _uid;
}

void AccountInfo::setName(char* name1){
    _userLoginName = name1;
}

void AccountInfo::setUID(unsigned int UID1){
    _uid = UID1;
}

class UserDB
{
private:
    AccountInfo* _accounts[200]; // store up to 200 accounts
    unsigned int _size; // number of account stored
    unsigned int _nextUid; // next user id to be assigned
    unsigned int _defaultGid; // default group id

public:
    UserDB();
    void adduser(AccountInfo* newUser);
    int size(); // return the number of accounts stored (_size)
    // and other public methods (mutators/setters, accessors/getters)
};

UserDB::UserDB(){
    //set variables
    _size = 0;
    _nextUid = 1001;
}

void UserDB::adduser(AccountInfo* newUser){
    // add a new user to _accounts
    _accounts[_size] = newUser;
    _size++; //increment _size.

    cout << newUser->getName() << " with " << newUser->getUID() << " is added." << endl;
}

int UserDB::size(){
    return _size; // the number of accounts stored
}

int main() {
    //variables 
    char buffer[256];
    AccountInfo *tempAccount;
    UserDB *users = new UserDB();
    char home_directory[33];
    int user_id;

    // other local variables used to store data temporally
    char c;
    int length = 0;
    int templength = 0; //index of buffer

    cin.get(c);

    while (!cin.eof()) // while end of line is not reached
    {
        //read a line into buffer
        if (c == '\n'){
            buffer[templength] = '[=11=]';

            if (buffer[0] == 'a'){
                tempAccount = new AccountInfo(buffer);
                users->adduser(tempAccount);
            }

            //clear the buffer array
            buffer[0] = '[=11=]';
            //reset the index of the buffer
            templength = 0;

            //incrementation
            length++;
            cin.get(c);
        }
        else{
            buffer[templength] = c;

            //incrementation 
            length++;
            templength++;
            cin.get(c);
        }
    }
    return 0;
}

_userLoginName来自setNamesetName来自setName(bufferLine);

而现在,bufferLine是一个局部变量,会在AccountInfo的构造函数完成后销毁。

所以下次你调用 getName 时它就爆炸了。

您可以将 char bufferLine[256]; 替换为 char* bufferLine = new char [256]