簡體   English   中英

在C ++中使用指向char的指針時,沒有輸出且返回錯誤為1

[英]No output with a return error of 1 while using pointers to char in c++

我已經徹底搜索了無濟於事的答案。 我一直在使用C ++ Gaddis入門教材作為參考。 我知道對這個問題有一個簡單的答案,但是我很難弄清楚。 這是我的代碼:

class Employee
{
    private:
    char *name;
    int idNumber;
    char *department;
    char *position;
    void initName(const char *n)
{
    name = new char[strlen(n) + 1];
    strcpy(name, n);
}
void initDepartment(char *d)
{
    department = new char[strlen(d) + 1];
    strcpy(department, d);
}

void initPosition(char *p)
{
    position = new char[strlen(p) + 1];
    strcpy(position, p);
}

public:

Employee()
{
    strcpy(name, "");
    idNumber = 0;
    strcpy(department, "");
    strcpy(position, "");

}
Employee(char *nam, int num, char *dep, char *posit)
{
    initName(nam);
    idNumber = num;
    initDepartment(dep);
    initPosition(posit);
}

Employee(const char *nam, int num)
{
    initName(nam);
    idNumber = num;
    strcpy(department, "");
    strcpy(position, "");   
}

~Employee()
{
    delete [] name;
    delete [] department;
    delete [] position;
}

void setName(char *n)
{
    name = new char[strlen(n) + 1];
    strcpy(name, n);
}

void setIdNumber(int num)
{
    idNumber = num;
}

void setDepartment(char *d)
{
    department = new char[strlen(d) + 1];
    strcpy(department, d);
}

void setPosition(char *p)
{
    position = new char[strlen(p) + 1];
    strcpy(position, p);
}

const char * getName() const
{
    return name;
}

int getIdNumber() const
{
    return idNumber;
}

const char * getDepartment() const 
{
    return department;
}

const char * getPosition() const 
{
    return position;
}
};

int main(int argc, char** argv) 
{
const int SIZE = 50;
const char name[SIZE] = "Mark Jones";
Employee employee(name, 3452);
const char *ptr = NULL;

ptr = employee.getName();

cout << ptr << endl;

return 0;
}

在默認構造函數中,將空字符串復制到垃圾桶指針:

Employee()
// name and all other members are not initialized
{
    strcpy(name, ""); // << copy to unallocated memory
    idNumber = 0;
    strcpy(department, ""); // << and here
    strcpy(position, ""); // << and here
}

正確的默認構造函數為:

Employee()
: name(nullptr) // Initialize all members here
, idNumber(0)
, department(nullptr)
, position(nullptr)
{
    // Initialize c-strings
    initName("");
    initDepartment("");
    initPosition("");
}

不要忘記一直使用const 以及類似的其他構造函數:

Employee(const char* nam, int num, const char* dep, const char* posit)
: name(nullptr) // << Initialize all members here
, idNumber(num) // << Initialize number with proper value
, department(nullptr)
, position(nullptr)
{
    initName(nam);
    initDepartment(dep);
    initPosition(posit);
}

Employee(const char* nam, int num)
: name(nullptr) // Initialize all members here
, idNumber(num) // << Initialize number with proper value
, department(nullptr)
, position(nullptr)
{
    initName(nam);
    initDepartment("");
    initPosition("");   
}

適當功能的參數:

void initName(const char *n)
{
    name = new char[strlen(n) + 1];
    strcpy(name, n);
}
void initDepartment(const char *d)
{
    department = new char[strlen(d) + 1];
    strcpy(department, d);
}

void initPosition(const char *p)
{
    position = new char[strlen(p) + 1];
    strcpy(position, p);
}

並且不要忘記清除設置​​器中的內存(否則它是內存泄漏):

void setName(const char *n)
{
    delete[] name;
    name = new char[strlen(n) + 1];
    strcpy(name, n);
}

void setDepartment(const char *d)
{
    delete[] department;
    department = new char[strlen(d) + 1];
    strcpy(department, d);
}

void setPosition(char *p)
{
    delete[] position;
    position = new char[strlen(p) + 1];
    strcpy(position, p);
}

這應該可以解決所有運行時錯誤。

而是在構造函數中使用成員函數。 構造函數啟動時,指針不會初始化,但是成員函數會初始化它們:

Employee(const char *nam,int num)
{
    initName(nam);
    idNumber = num;
    //strcpy(department,"");
    //strcpy(position,"");
    initDepartment("");
    initPosition("");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM