我正在尝试使用Visual Studio 2017构建包含以下文件的程序:main.cpp,authenticate.cpp和authenticate.h(它们均在源文件中)。该程序仅检查用户名和密码的输入。它的主要目的是使用未命名的命名空间。
authenticate.h
#include<iostream>
#include<string>
#ifndef AUTHENTICATE_H
#define AUTHENTICATE_H
namespace authenticate {
void inputUserName();
void inputPassword();
std::string getUserName();
std::string getPassword();
}
#endif // !AUTHENTICATE_H
authenticate.cpp
#include<iostream>
#include<string>
#include"authenticate.h"
namespace { // practice using unnamed namespace.
std::string username;
std::string password;
bool nameValid() { // check if 8 letters were entered
bool allLetter = true;
for (unsigned int x = 0; x < password.size(); x++) if (isalpha(password[x])) {
allLetter = false;
break;
}
return(username.size() == 8 && allLetter);
}
bool passValid() {// check if 8 characters including at least 1 non-letter were entered
bool allLetter = true;
for (unsigned int x = 0; x < password.size(); x++) if (isalpha(password[x])) {
allLetter = false;
break;
}
return((password.size() == 8) && !allLetter);
}
}
main.cpp
#include<iostream>
#include"authenticate.h"
int main()
{
authenticate::inputUserName();
authenticate::inputPassword();
std::cout << "Your username is "
<< authenticate::getUserName()
<<" and your password is: "
<<authenticate::getPassword() << std::endl;
return 0;
}
在遇到这些错误之前,我已按照之前的说明进行操作,并将入口点设置为 main.cpp ,将子系统设置为 CONSOLE 。 当我编译每个文件时,没有错误。当我生成程序时出现错误。它们都是LNK 2001和LNK 2019。
**Error** LNK2001 unresolved external symbol _main.cpp
我的猜测是由于该程序无法启动,因此我在约定上犯了一个错误。