C++ C++;指针数组为每个元素返回相同的值
我编写了一个简化版本的代码来说明我遇到的一个问题。我认为C++ C++;指针数组为每个元素返回相同的值,c++,arrays,pointers,C++,Arrays,Pointers,我编写了一个简化版本的代码来说明我遇到的一个问题。我认为main()中存在错误,导致showUsers()方法为每个元素输出相同的登录/密码组合(始终是最后添加的一个) 这是我的密码: …我得到了期望的输出 非常感谢。您的问题是,您的用户帐户(全部)最终成为指向main()中(单个)用户和密码字符数组的指针。 您可以通过每次使用此main创建一个新阵列来修复此问题: //===============================================================
main()
中存在错误,导致showUsers()
方法为每个元素输出相同的登录/密码组合(始终是最后添加的一个)
这是我的密码:
…我得到了期望的输出
非常感谢。您的问题是,您的用户帐户(全部)最终成为指向main()中(单个)用户和密码字符数组的指针。 您可以通过每次使用此main创建一个新阵列来修复此问题:
//=============================================================================
int main() {
char buffer[256];
char *userLoginName;
char *password;
int i, j, k;
char flag[3];;
char command[11];
char blank = ' ';
UsersDB* users = new UsersDB();
AccountInfo* tempAccount;
while (!cin.eof()) { //while end of line is not reached
cin.getline(buffer, 256);
k = getNextToken(buffer, command, 0, 256, 10, blank);
if (command[0] == 'a') {
userLoginName = new char[9];
password = new char[17];
tempAccount = new AccountInfo();
k = getNextToken(buffer, userLoginName, k, 256, 8, blank);
(*tempAccount).setUsername(userLoginName);
k = getNextToken(buffer, flag, k, 256, 2, blank);
if (flag[1] == 'p') {
k = getNextToken(buffer, password, k, 256, 16, blank);
(*tempAccount).setPassword(password);
}
cout << *tempAccount << endl;
(*users).addUser(tempAccount);
}
else if (command[0] == 's') {
(*users).showUsers();
}
else cout << "Command not found." << endl;
}
return 0;
}
/=========================================================================================================================================================================================================================================================================================================================================================================================================
int main(){
字符缓冲区[256];
char*userLoginName;
字符*密码;
int i,j,k;
字符标志[3];;
char命令[11];
字符空白=“”;
UsersDB*users=newusersdb();
账户信息*临时账户;
while(!cin.eof()){//while未到达行尾
cin.getline(缓冲区,256);
k=getNextToken(缓冲区,命令,0,256,10,空白);
如果(命令[0]=“a”){
userLoginName=新字符[9];
密码=新字符[17];
tempAccount=新AccountInfo();
k=getNextToken(缓冲区,用户登录名,k,256,8,空白);
(*tempAccount.setUsername(userLoginName);
k=getNextToken(缓冲区,标志,k,256,2,空白);
如果(标志[1]=“p”){
k=getNextToken(缓冲区,密码,k,256,16,空白);
(*tempAccount).setPassword(密码);
}
cout您的问题是您的UserAccounts(所有)最终都是指向main()中(单个)用户和密码字符数组的指针。
您可以通过每次使用此main创建一个新阵列来修复此问题:
//=============================================================================
int main() {
char buffer[256];
char *userLoginName;
char *password;
int i, j, k;
char flag[3];;
char command[11];
char blank = ' ';
UsersDB* users = new UsersDB();
AccountInfo* tempAccount;
while (!cin.eof()) { //while end of line is not reached
cin.getline(buffer, 256);
k = getNextToken(buffer, command, 0, 256, 10, blank);
if (command[0] == 'a') {
userLoginName = new char[9];
password = new char[17];
tempAccount = new AccountInfo();
k = getNextToken(buffer, userLoginName, k, 256, 8, blank);
(*tempAccount).setUsername(userLoginName);
k = getNextToken(buffer, flag, k, 256, 2, blank);
if (flag[1] == 'p') {
k = getNextToken(buffer, password, k, 256, 16, blank);
(*tempAccount).setPassword(password);
}
cout << *tempAccount << endl;
(*users).addUser(tempAccount);
}
else if (command[0] == 's') {
(*users).showUsers();
}
else cout << "Command not found." << endl;
}
return 0;
}
/=========================================================================================================================================================================================================================================================================================================================================================================================================
int main(){
字符缓冲区[256];
char*userLoginName;
字符*密码;
int i,j,k;
字符标志[3];;
char命令[11];
字符空白=“”;
UsersDB*users=newusersdb();
账户信息*临时账户;
while(!cin.eof()){//while未到达行尾
cin.getline(缓冲区,256);
k=getNextToken(缓冲区,命令,0,256,10,空白);
如果(命令[0]=“a”){
userLoginName=新字符[9];
密码=新字符[17];
tempAccount=新AccountInfo();
k=getNextToken(缓冲区,用户登录名,k,256,8,空白);
(*tempAccount.setUsername(userLoginName);
k=getNextToken(缓冲区,标志,k,256,2,空白);
如果(标志[1]=“p”){
k=getNextToken(缓冲区,密码,k,256,16,空白);
(*tempAccount).setPassword(密码);
}
cout您在类中使用char*指针,它们的工作方式与“普通”不同字符串。当您将char*指针传递给方法并将其分配给成员变量时,您不是在复制字符串,而是使成员变量指向与参数指向的字符串相同的字符串。由于传入的所有参数都是指向输入缓冲区的指针,缓冲区中的下一个输入将覆盖所有字符串中的文本以前的对象
在C++中,你应该真正使用而不是绕过char *来进行字符串操作,这将在很大程度上帮助你在这种情况下,并自动处理复制和分配新字符串的内存。
后一个版本使用常量的原因是,您的对象最终指向的文本常量不会像缓冲区那样被覆盖。如果在类中使用char*指针,它们的工作方式与“普通”不同字符串。当您将char*指针传递给方法并将其分配给成员变量时,您不是在复制字符串,而是使成员变量指向与参数指向的字符串相同的字符串。由于传入的所有参数都是指向输入缓冲区的指针,缓冲区中的下一个输入将覆盖所有字符串中的文本以前的对象
在C++中,你应该真正使用而不是绕过char *来进行字符串操作,这将在很大程度上帮助你在这种情况下,并自动处理复制和分配新字符串的内存。
后一个版本使用常量的原因是,您的对象最终指向的文本常量不会像缓冲区那样被覆盖。您从不在AccountInfo
类中分配空间来存储用户名和密码。您的setUserName
和setPassword
函数只需复制他将原始指针指向类。因此,在第一个main
中,每个用户只需指向您在main
中声明的username
和userLoginName
缓冲区,因此它们都是相同的。在第二个main
中,它们指向您用来构造它们的字符串文本
<>我也会补充说,这是一个具有C成员函数的C代码。如果你使用C++,你应该使用C++的特性,比如:代码> STD::String 和 STD::vector 。这样的错误几乎不可能。 < P>你在你的<代码>帐户信息> /代码>类中永远不分配空间来存储用户名和密码。etUserName
和setPassword
功能简单
===============================================================================
>adduser bob -p password1
Login: bob
Password: password1
>adduser jack -p mypassword
Login: jack
Password: mypassword
>adduser jill -p pass1234
Login: jill
Password: pass1234
>showusers
Login: bob
Password: password1
Login: jack
Password: mypassword
Login: jill
Password: pass1234
===============================================================================
//=============================================================================
int main() {
UsersDB* users = new UsersDB();
AccountInfo* tempAccount;
tempAccount = new AccountInfo("jack", "mypassword");
(*users).addUser(tempAccount);
tempAccount = new AccountInfo("jill", "pass1234");
(*users).addUser(tempAccount);
(*users).showUsers();
return 0;
}
//=============================================================================
int main() {
char buffer[256];
char *userLoginName;
char *password;
int i, j, k;
char flag[3];;
char command[11];
char blank = ' ';
UsersDB* users = new UsersDB();
AccountInfo* tempAccount;
while (!cin.eof()) { //while end of line is not reached
cin.getline(buffer, 256);
k = getNextToken(buffer, command, 0, 256, 10, blank);
if (command[0] == 'a') {
userLoginName = new char[9];
password = new char[17];
tempAccount = new AccountInfo();
k = getNextToken(buffer, userLoginName, k, 256, 8, blank);
(*tempAccount).setUsername(userLoginName);
k = getNextToken(buffer, flag, k, 256, 2, blank);
if (flag[1] == 'p') {
k = getNextToken(buffer, password, k, 256, 16, blank);
(*tempAccount).setPassword(password);
}
cout << *tempAccount << endl;
(*users).addUser(tempAccount);
}
else if (command[0] == 's') {
(*users).showUsers();
}
else cout << "Command not found." << endl;
}
return 0;
}
//At top of file:
#include <string.h>
AccountInfo::AccountInfo() {
_username = new char[9];
_password = new char[17];
}
AccountInfo::AccountInfo(char* username, char* password) {
strcpy(_username, username);
strcpy(_password, password);
}
AccountInfo::~AccountInfo() {
delete _username;
delete _password;
}
void AccountInfo::setUsername(char* username) {
strcpy(_username, username);
}
void AccountInfo::setPassword(char* password) {
strcpy(_password, password);
}