方法调用返回null值

时间:2015-01-26 06:07:16

标签: c++ pointers getter-setter

以下代码正常运行。但是,在调用getName()方法时,在UserDB类方法adduser(AccountInfo * newUser)中,返回null值。我确定指针存在问题,但是在审阅后代码似乎没问题。方法调用不正确吗?

#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] != '\0'){
        length1++;
    }
    //separate the text by white space
    for (int i = 8; i < length1 + 1; i++){
        bufferLine[tempLength1 + 1] = '\0';

        if ((_line[i] == ' ') || (_line[i] == '\0'))
        {

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

                if (bufferLine[1] == 'u'){
                    bufferLine[0] = '\0';
                    tempLength1 = 0;
                    i++;
                    while (_line[i] != '-'){
                        bufferLine[tempLength1] = _line[i];
                        i++;
                        tempLength1++;
                    }
                    bufferLine[tempLength1] = '\0';
                    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] = '\0';
                setName(bufferLine);
            }
            bufferLine[0] = '\0'; //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] = '\0';

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

            //clear the buffer array
            buffer[0] = '\0';
            //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;
}

1 个答案:

答案 0 :(得分:2)

_userLoginName来自setNamesetName来自setName(bufferLine);;

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

因此,下次您致电getName时,它会爆炸。

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