-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;
}

Above is my cpp file's code. When I compile it,

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)

The errors are caused by uninstantiated static member variables in the class, so I wonder how to fix the problem.

SOLUTION: C++ static member variable and its initialization

Community
  • 1
  • 1
Xiao
  • 1
  • 2
  • 13
    "A:\"...wow, is that actually a *floppy disk*? – lc. Oct 11 '12 at 06:49
  • 2
    `username` and the other members are static. If that's what you want you have to instantiate them separately outside the class. But are you sure that is what you want? Since the class is named `User`, it would seem more natural to have a regular (non-static) member `username`. – jogojapan Oct 11 '12 at 06:52
  • @lc.: It's NOT a floppy disk. Users could change the letter of partitions. – Xiao Sep 18 '14 at 08:58

1 Answers1

1

You should explicitly instantiate your static variables the same way you've done it for 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];
nogard
  • 9,432
  • 6
  • 33
  • 53