我在c ++中遇到strcmp函数问题,编译器说"错误读取字符串"而且我确实使用了字符串..如果你能看一眼我会更加精彩
///这是使用函数的地方,数据是类MailAcount //
cout << "please enter user name: " << endl;
char input_user[20];
cin >> input_user;
cout << "please enter password: " << endl;
char input_password[20];
cin >> input_password;
if (!strncmp(input_user, data.GetUser(), 20) ||
!strncmp(input_password, data.GetPassword(), 20))
cout << "ERROR" << endl;
else
{
cout << "Access confirm" << endl;
}
//这是MailAcount //
的标题 class MailAcount
{
private:
char* _email;
char* _password;
public:
MailAcount(char* email,char* password);
MailAcount();
char* GetUser();
char* GetPassword();
~MailAcount();
};
//这是MailAcount //
的cpp#include "MailAcount.h"
#include <iostream>
using namespace std;
MailAcount::MailAcount(char *email,char *password)
{
_email = email;
_password = password;
}
MailAcount::MailAcount()
{
}
char* MailAcount::GetUser()
{
return _email;
}
char* MailAcount::GetPassword()
{
return _password;
}
MailAcount::~MailAcount()
{
}
答案 0 :(得分:-1)
程序在调试器中崩溃很可能是由于你没有为你的邮件帐户指针指向的字符提供内存的情况。
这个有效
char myName[20]; // 20 characters on the stack
char myPwd[20]; // 20 characters on the stack
MailAccount(myName, myPwd); // ok, points to allocated memory (on the stack)
这个会崩溃
char* myName; // only a pointer pointing somewhere
char* myPwd; // only a pointer pointing somewhere
MailAccount(myName, myPwd); // not ok, as the pointers will point (most likely) to non accessible memory
编辑:当您将其标记为C ++时,您仍应考虑使用std库字符串作为已建议的评论者。 EDIT2:更正了评论