c++ - 方法调用返回空值

标签 c++ pointers getter-setter

以下代码可以正常工作。但是,在调用 getName() 方法时,在 UserDB 类方法 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] != '\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;
}

最佳答案

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

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

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

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

关于c++ - 方法调用返回空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28145435/

相关文章:

flutter - Dart 中 getter 和常规方法的区别

c++ - 在C++中从exe中获取参数

c++ - 在 lambda 中是否有记录函数名称的好方法?

pointers - slice 结构行为

c - 如何使用 fwrite 将指针数组的内容写入文件?

Angular 2 setter 和 getter

class - 让我的 getter 方法更改存储的值是不好的做法吗?

c++ - 如何使用 wxWidgets C++ 加载 gif?

c++ - C++中的复制构造函数和临时对象

c - 调用函数时出现不兼容指针类型错误