-1
class User
{
public:
    User(char *name,char *pass)
    {
        strncpy(username[num],name,strlen(name));
        username[num][strlen(name)]='\0';
        for(int i=0;i<strlen(pass);i++)
            password[num][i]=(i+pass[i])%256;
        password[num][strlen(pass)+1]='\0';
        num++;
    }
    static void AddUser(char *name,char *pass)
    {
        strncpy(username[num],name,strlen(name));
        username[num][strlen(name)]='\0';
        for(int i=0;i<strlen(pass);i++)
            password[num][i]=(i+pass[i])%256;
        password[num][strlen(pass)+1]='\0';
        num++;
    }
    static int loggin(char *name,char *pass)
    {
        for(int i=0;i<num;i++)
            if(strcmp(username[i],name)==0)
            {
                for(int j=0;pass[j]!='\0';j++)
                    if(password[i][j]!=(j+pass[j])%256)
                        return -1;
                return i;
            }
        return -1;
    }
private:
    static char username[10][20];
    static char password[10][20];
    static int num;
};

int User::num=0;

int main()
{
    char name[20],pass[20];
    User u1("LiWei","liwei99");
    u1.AddUser("ChenHanfu","20090911");
    User::AddUser("Zhanggaolin","198845");
    cout<<"Input username:";
    cin>>name;
    cout<<"Input password:";
    cin>>pass;
    if(User::loggin(name,pass)>=0)
        cout<<"success login!"<<endl;
    else
        cout<<"login fail!"<<endl;
    return 0;
}

以上是我的 cpp 文件的代码。当我编译它时,

Compiling source file(s)...
    4.cpp
    4.cpp: In constructor `User::User(char*, char*)':
    4.cpp:13: warning: comparison between signed and unsigned integer expressions
    4.cpp: In static member function `static void User::AddUser(char*, char*)':
    4.cpp:22: warning: comparison between signed and unsigned integer expressions
    Linking...
    A:\Homework\CPP\Stu\Debug\4.o: In function `ZN4UserC1EPcS0_':
    A:\Homework\CPP\Stu\4.cpp:(.text$_ZN4User6logginEPcS0_[User::loggin(char*, char*)]+0x2b): undefined reference to `User::username'
    A:\Homework\CPP\Stu\4.cpp:(.text$_ZN4User6logginEPcS0_[User::loggin(char*, char*)]+0x65): undefined reference to `User::password'
    A:\Homework\CPP\Stu\4.cpp:(.text$_ZN4User7AddUserEPcS0_[User::AddUser(char*, char*)]+0x2d): undefined reference to `User::username'
    A:\Homework\CPP\Stu\4.cpp:(.text$_ZN4User7AddUserEPcS0_[User::AddUser(char*, char*)]+0x59): undefined reference to `User::username'
    A:\Homework\CPP\Stu\4.cpp:(.text$_ZN4User7AddUserEPcS0_[User::AddUser(char*, char*)]+0x8b): undefined reference to `User::password'
    A:\Homework\CPP\Stu\4.cpp:(.text$_ZN4User7AddUserEPcS0_[User::AddUser(char*, char*)]+0xf2): undefined reference to `User::password'
    A:\Homework\CPP\Stu\4.cpp:(.text$_ZN4UserC1EPcS0_[User::User(char*, char*)]+0x2d): undefined reference to `User::username'
    A:\Homework\CPP\Stu\4.cpp:(.text$_ZN4UserC1EPcS0_[User::User(char*, char*)]+0x59): undefined reference to `User::username'
    A:\Homework\CPP\Stu\4.cpp:(.text$_ZN4UserC1EPcS0_[User::User(char*, char*)]+0x8b): undefined reference to `User::password'
    A:\Homework\CPP\Stu\4.cpp:(.text$_ZN4UserC1EPcS0_[User::User(char*, char*)]+0xf2): undefined reference to `User::password'
    collect2: ld returned 1 exit status

    Student.exe - 10 error(s), 2 warning(s)

错误是由类中未实例化的静态成员变量引起的,所以我想知道如何解决这个问题。

解决方案: C++ 静态成员变量及其初始化

4

1 回答 1

1

您应该以与您相同的方式显式实例化静态变量num

private:
    static char username[10][20];
    static char password[10][20];
    static int num;
};

int User::num=0;
char User::username[10][20];
char User::password[10][20];
于 2012-10-11T06:51:28.800 回答