[英]c++ set const char* member variable of class
When I am trying to assign a member variable, which is const char*, it just ends the program.当我试图分配一个成员变量时,它是 const char*,它只是结束程序。 However, if I get some cin inputs before I assign a member variable, it works.但是,如果我在分配成员变量之前得到一些 cin 输入,它就可以工作。 this is my NameCard.h这是我的 NameCard.h
class NameCard {
private:
const char* name;
const char* companyName;
const char* phoneNumber;
enum grade{CLERK, SENIOR, ASSIST, MANAGER};
grade my_grade;
NameCard* nextCard;
NameCard* head;
public:
NameCard(const char*, const char*, const char*, int);
void setName();
void setCompanyName();
void setPhoneNumber();
void setMyGrade();
void setHead(NameCard*);
void setNextCard(NameCard*);
void findInfo(char*);
void printAll();
};
This is main.cpp这是 main.cpp
#include <iostream>
#include <cstring>
#include "NameCard.h"
using namespace std;
NameCard::NameCard(const char* a, const char* b, const char* c, int d){
name = a;
companyName = b;
phoneNumber = c;
switch(d){
case CLERK: my_grade = CLERK; break;
case SENIOR: my_grade = SENIOR; break;
case ASSIST: my_grade = ASSIST; break;
case MANAGER: my_grade = MANAGER; break;
}
cout << name << " " << companyName << " " << phoneNumber << " " << my_grade << endl;
}
void NameCard::setName(){
char* name;
cout << "type name." << endl;
cin >> name;
this->name = name;
cout << this->name << endl;
}
void NameCard::setCompanyName(){
char* company_name;
cout << "type company name." << endl;
cin >> company_name;
this->companyName = company_name;
cout << this->companyName << endl;
}
void NameCard::setPhoneNumber(){
char* phone_number;
cout << "type phone number." << endl;
cin >> phone_number;
phoneNumber = phone_number;
cout << phoneNumber;
};
void NameCard::setMyGrade(){
int input_grade;
cout << "type position. 1)CLERK 2) SENOIR 3) ASSIST 4)MANAGER" << endl;
cin >> input_grade;
switch(input_grade){
case CLERK: my_grade = CLERK; break;
case SENIOR: my_grade = SENIOR; break;
case ASSIST: my_grade = ASSIST; break;
case MANAGER: my_grade = MANAGER; break;
}
cout << input_grade;
};
void NameCard::setHead(NameCard* head){
this->head = head;
};
void NameCard::setNextCard(NameCard* next){
this->nextCard = next;
}
void NameCard::findInfo(char* target_name){
NameCard* temp;
temp = head;
while(temp != NULL){
if(temp->name == target_name){
cout << "name : " << temp->name << endl;
cout << "companyName : " << temp->companyName << endl;
cout << "phoneNumber : " << temp->phoneNumber << endl;
cout << "grade : " << temp->my_grade << endl;
break;
}
temp = temp->nextCard;
}
};
void NameCard::printAll(){
NameCard* temp;
temp = head;
while(temp!= NULL){
cout<< "----------------------------" << endl;
cout << "name : " << temp->name << endl;
cout << "companyName : " << temp->companyName << endl;
cout << "phoneNumber : " << temp->phoneNumber << endl;
cout << "grade : " << temp->my_grade << endl;
temp = temp->nextCard;
}
}
int main(){
int index = 0;
NameCard* head = new NameCard("test", "test", "test", 3);
NameCard* bef = NULL;
// char input[50];
// cout << "nmae ";
// cin >>input;
head->setName();
head->setPhoneNumber();
head->setCompanyName();
head->setMyGrade();
return 0;
}
And the problem is here.问题就在这里。 This is not working.这是行不通的。 The program ends right after it gets name from setName().程序在从 setName() 获得名称后立即结束。 It just ends at the setName() cin >> name.它以 setName() cin >> name 结束。 it did not print name after then, and stopped.在那之后它没有打印名称,并停止了。
int main(){
int index = 0;
NameCard* head = new NameCard("test", "test", "test", 3);
NameCard* bef = NULL;
// char input[50];
// cout << "nmae ";
// cin >>input;
head->setName();
head->setPhoneNumber();
head->setCompanyName();
head->setMyGrade();
return 0;
}
However, this is working fine.但是,这工作正常。 it gets the test input and executes all the code below.它获取测试输入并执行下面的所有代码。
int main(){
int index = 0;
NameCard* head = new NameCard("test", "test", "test", 3);
NameCard* bef = NULL;
char input[50];
cout << "test input ";
cin >>input;
head->setName();
head->setPhoneNumber();
head->setCompanyName();
head->setMyGrade();
return 0;
}
You are writing to memory you didn't allocate and most likely don't own.您正在写入未分配且很可能不拥有的内存。 Check this for example例如检查这个
void NameCard::setName(){
char* name;
cout << "type name." << endl;
cin >> name;
this->name = name;
cout << this->name << endl;
}
You declare char* name
and try to read from cin
into it.你声明char* name
并尝试从cin
读入它。 But name
is just an uninitialized pointer.但name
只是一个未初始化的指针。
One fix may be to declare name
as an array, which also decays to a pointer (so it can be used as a pointer), but does have associated memory.一种解决方法可能是将name
声明为一个数组,该数组也衰减为一个指针(因此它可以用作指针),但确实具有相关联的内存。 Like this像这样
char name[50];
Or if you want it to be dynamically allocated或者,如果您希望它被动态分配
char* name = new char[50];
// Use it and when you are done delete it
// Never forget to release memory allocated with new or you get a memory leak
delete[] name;
The key point is that the pointer needs to point to some memory block you know you can use.关键是指针需要指向一些你知道可以使用的内存块。 Note that in the example I allocated 50 chars, you have to make sure you never use (read/write) past the allocated memory, or you get the same error you have been getting and potentially program termination.请注意,在我分配了 50 个字符的示例中,您必须确保您永远不会使用(读/写)超过分配的内存,否则您会遇到相同的错误并可能导致程序终止。
A safer alternative is to use std::string
, which automatically handles memory for you, so you don't have to mind about allocation and releasing.更安全的替代方法是使用std::string
,它会自动为您处理内存,因此您不必担心分配和释放。 You could do something like你可以做类似的事情
std::string name;
std::cin >> name;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.