简体   繁体   中英

C++ trouble with extern - undefined reference

main.cpp:

bool lgstatus;
User currentUser;
//...
int main(){ //... }

loginwindow.cpp:

void LoginWindow::on_cmdCreate_clicked()
{
  extern bool lgstatus;
  extern User currentUser;
  //...
  currentUser.setMail(ui->txtAccountMail->text().toStdString());
  currentUser.setName(ui->txtAccountName->text().toStdString());
  currentUser.setPassword(ui->txtAccountPassword->text().toStdString());
  //...
  lgstatus = true;
}

My class User has 3 functions. Each of them takes a string as argument. I don't know whats wrong. The compiler doesn't complain if I change lgstatus but my currenUser .

Class :

class User
{
public:
    User();
    User(const std::string &name, const std::string &password);
    User(const std::string &name, const std::string &password, const std::string &mail);

    void setName(const std::string &name);
    void setMail(const std::string &mail);
    void setPassword(const std::string &password);

private:
    std::string user_name;
    std::string user_password;
    std::string user_mail;
};

The "set" functions simply pass their argument to the user_name etc. I don't think it would be necessary to show them as well.

Errors :

  • undefined reference to 'User::setMail(std::string const&)'

  • undefined reference to 'User::setName(std::string const&)'

  • undefined reference to `User::setPassword(std::string const&)'

What did I do wrong?

More than likely you are not doing the correct #include in your loginwindow.cpp. As a result, the compiler never finds the correct functions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM