C++ LNK 2019和2001错误
所以,我以为我要完成我的计划了。我编译了它,得到了一堆LNK(2019年和2001年)错误 我对它们做了一些研究,一个流行的解决方案是删除导致错误的代码。这似乎并不完全正确。我试过了,但没用 这是错误从中分支的文件 我只是想看看如何修复这些错误。我以前从未遇到过LNK错误 错误: 1> bankingdriver.obj:错误LNK2019:未解析的外部符号“public:\u thiscall Account::~Account(void)”(??1 Account@@QAE@XZ)在函数“void\uu cdecl createTextFile(类std::basic\u fstream>&)”中引用(?createTextFile@@YAXAAV?$basic_fstream@DU?$char_traits@D@std@@@std@@@Z) 1> bankingdriver.obj:错误LNK2019:未解析的外部符号“void\u cdecl outputLine(class std::basic\u ostream>&,class Account const&)”(?outputLine@@YAXAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@@ABVAccount@@@Z)在函数“void\uu cdecl createTextFile(类std::basic\u fstream>&)”中引用(?createTextFile@@YAXAAV?$basic)_fstream@DU?$char_traits@D@std@@@std@@@Z) 1> bankingdriver.obj:错误LNK2019:未解析的外部符号“public:double\u thiscall Account::getBalance(void)”(?getBalance@Account@@QAENXZ)在函数“void\uuuCDECL updateRecord(class std::basic\u fstream>&)”中引用(?updateRecord@@YAXAAV?$basic_fstream@DU?$char_traits@D@std@@@std@@@Z) 1> bankingdriver.obj:错误LNK2019:未解析的外部符号“public:void\uu thiscall Account::setAccountNumber(int)”(?setAccountNumber@Account@@QAEXH@Z)在函数“void\uu cdecl newRecord(class std::basic\u fstream>&)”中引用(?newRecord@@YAXAAV?$basic_fstream@DU?$char_traits@D@std@@@std@@@Z) 1> BankingSystem.obj:错误LNK2001:未解析的外部符号“public:void u thiscall Account::setAccountNumber(int)”(?setAccountNumber@Account@@QAEXH@Z)C++ LNK 2019和2001错误,c++,visual-c++,C++,Visual C++,所以,我以为我要完成我的计划了。我编译了它,得到了一堆LNK(2019年和2001年)错误 我对它们做了一些研究,一个流行的解决方案是删除导致错误的代码。这似乎并不完全正确。我试过了,但没用 这是错误从中分支的文件 我只是想看看如何修复这些错误。我以前从未遇到过LNK错误 错误: 1> bankingdriver.obj:错误LNK2019:未解析的外部符号“public:\u thiscall Account::~Account(void)”(??1 Account@@QAE@XZ)在函数“v
//银行系统驱动程序
#包括“BankingSystem.h”//Account类定义
#包括
#包括
#包括
#include//exit函数原型
使用名称空间std;
int enterChoice();
作废createTextFile(fstream&);
作废更新记录(fstream&);
作废新记录(fstream&);
作废删除记录(fstream&);
无效输出行(ostream&,const Account&);
int GETCOUNT(常量字符*常量);
枚举选项{PRINT=1,UPDATE,NEW,DELETE,END};
int main()
{
//打开文件进行读写
fstream inOutCredit(“credit.dat”,ios::in | ios::out | ios::binary);
//如果fstream无法打开文件,请退出程序
如果(!inOutCredit)
{
cerr您应该检查“BankingSystem.h”和其中的Account类。
这里的错误意味着编译器没有找到这些函数地址,例如“getBalance”,因此您应该首先检查这些函数是否存在,然后必须确保BankingSystem.lib文件已包含在您的项目中。这些错误意味着这些函数已定义,但未声明。因此编译器已看到fu的定义选项是,但函数尚未在项目中实现。可能它们存在于库中,或者项目中缺少CPP文件。编译器获取源文件并生成目标文件。链接器获取所有目标文件(和库)并生成可执行文件(或DLL)。您收到的消息表明,您可能忘记为这些函数编写代码,或者在链接步骤中忘记包含必要的对象文件或库
这似乎是MS VC++。如果您使用的是命令行工具(或类似于使用命令行工具的NMAKE),然后请注意,默认情况下,cl
命令将编译并尝试链接生成的文件。这对于快速实验很好,但在项目跨越多个源文件时很少有用
我猜您只是在尝试编译一个文件,而cl
也在尝试为您链接它。要使cl
只执行编译步骤,请使用/c
选项。然后,将所有源文件编译成目标文件后,您可以使用链接将它们作为单独的步骤链接到逗号nd并列出所有对象文件
如果您使用Visual Studio IDE进行构建,则需要将所有源文件添加到同一项目。是否链接了BankingSystem库?在库中->输入选项?对不起,您的意思是什么?“我对它们进行了一些研究,一个流行的解决方案是删除导致错误的代码。”-为什么停在那里?只需删除所有代码,我保证错误会消失。可能是重复的,还有十几个提到您忘记包含.lib
文件。实际上,这意味着链接器尚未找到这些函数。
//banking system driver program
#include "BankingSystem.h" // Account class definition
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib> // exit function prototype
using namespace std;
int enterChoice();
void createTextFile( fstream& );
void updateRecord( fstream& );
void newRecord( fstream& );
void deleteRecord( fstream& );
void outputLine( ostream&, const Account & );
int getAccount( const char * const );
enum Choices { PRINT = 1, UPDATE, NEW, DELETE, END };
int main()
{
// open file for reading and writing
fstream inOutCredit( "credit.dat", ios::in | ios::out | ios::binary );
// exit program if fstream cannot open file
if ( !inOutCredit )
{
cerr << "File could not be opened." << endl;
exit ( 1 );
} // end if
int choice; // store user choice
// enable user to specify action
while ( ( choice = enterChoice() ) != END )
{
switch ( choice )
{
case PRINT: // create text file from record file
createTextFile( inOutCredit );
break;
case UPDATE: // update record
updateRecord( inOutCredit );
break;
case NEW: // create record
newRecord( inOutCredit );
break;
case DELETE: // delete existing record
deleteRecord( inOutCredit );
break;
default: // display error if user does not select valid choice
cerr << "Incorrect choice" << endl;
break;
} // end switch
inOutCredit.clear(); // reset end-of-file indicator
} // end while
} // end main
// enable user to input menu choice
int enterChoice()
{
// display available options
std::cout << "\nEnter your choice" << endl
<< "1 - store a formatted text file of accounts" << endl
<< "2 - called \"print.txt\" for printing" << endl
<< "3 - update an account" << endl
<< "4 - add a new account" << endl
<< "5 - delete an account" << endl
<< "6 - end program\n? ";
int menuChoice;
std::cin >> menuChoice; // input menu selection from user
return menuChoice;
} // end function enterChoice
// create formatted text file for printing
void createTextFile( fstream &readFromFile )
{
// create text file
ofstream outPrintFile( "print.txt", ios::out );
// exit program if ofstream cannot create file
if ( !outPrintFile )
{
cerr << "File could not be created." << endl;
exit( 1 );
} // end if
outPrintFile << left << setw( 10 ) << "Account" << setw( 16 )
<< "Last Name" << setw( 11 ) << "First Name" << right
<< setw( 10 ) << "Balance" << endl;
// set file-position pointer to beginning of readFromFile
readFromFile.seekg( 0 );
// read first record from record file
Account client;
readFromFile.read( reinterpret_cast< char * >( &client ),
sizeof( Account ) );
// copy all records from record file into text file
while ( !readFromFile.eof() )
{
// write single record to text file
if ( client.getAccountNumber() != 0 ) // skip empty records
outputLine( outPrintFile, client );
// read next record from record file
readFromFile.read( reinterpret_cast< char * >( &client ),
sizeof( Account ) );
} // end while
} // end function createTextFile
// update balance in record
void updateRecord( fstream &updateFile )
{
// obtain number of account to update
int accountNumber = getAccount( "Enter account to update" );
// move file-position pointer to correct record in file
updateFile.seekg( ( accountNumber - 1 ) * sizeof( Account ) );
// read first record from file
Account client;
updateFile.read( reinterpret_cast< char * >( &client ),
sizeof( Account ) );
// update record
if ( client.getAccountNumber() != 0 )
{
outputLine( cout, client ); // display the record
// request user to specify transaction
std::cout << "\nEnter charge (+) or payment (-): ";
double transaction; // charge or payment
std::cin >> transaction;
// update record balance
double oldBalance = client.getBalance();
client.setBalance( oldBalance + transaction );
outputLine( cout, client ); // display the record
// move file-position pointer to correct record in file
updateFile.seekp( ( accountNumber - 1 ) * sizeof( Account ) );
// write updated record over old record in file
updateFile.write( reinterpret_cast< const char * >( &client ),
sizeof( Account ) );
} // end if
else // display error if account does not exist
cerr << "Account #" << accountNumber
<< " has no information." << endl;
} // end function updateRecord
// create and insert record
void newRecord( fstream &insertInFile )
{
// obtain number of account to create
int accountNumber = getAccount( "Enter new account number" );
// move file-position pointer to correct record in file
insertInFile.seekg( ( accountNumber - 1 ) * sizeof( Account ) );
// read record from file
Account client;
insertInFile.read( reinterpret_cast< char * >( &client ),
sizeof( Account ) );
// create record, if record does not previously exist
if ( client.getAccountNumber() == 0 )
{
string lastName;
string firstName;
double balance;
// user enters last name, first name and balance
std:: cout << "Enter lastname, firstname, balance\n? ";
std::cin >> lastName;
std::cin >> firstName;
std::cin >> balance;
// use values to populate account values
client.setLastName( lastName );
client.setFirstName( firstName );
client.setBalance( balance );
client.setAccountNumber( accountNumber );
// move file-position pointer to correct record in file
insertInFile.seekp( ( accountNumber - 1 ) * sizeof( Account ) );
// insert record in file
insertInFile.write( reinterpret_cast< const char * >( &client ),
sizeof( Account ) );
} // end if
else // display error if account already exists
cerr << "Account #" << accountNumber
<< " already contains information." << endl;
} // end function newRecord
// delete an existing record
void deleteRecord( fstream &deleteFromFile )
{
// obtain number of account to delete
int accountNumber = getAccount( "Enter account to delete" );
// move file-position pointer to correct record in file
deleteFromFile.seekg( ( accountNumber - 1 ) * sizeof( Account ) );
// read record from file
Account client;
deleteFromFile.read( reinterpret_cast< char * >( &client ),
sizeof( Account ) );
// delete record, if record exists in file
if ( client.getAccountNumber() != 0 )
{
Account blankClient; // create blank record
// move file-position pointer to correct record in file
deleteFromFile.seekp( ( accountNumber - 1 ) *
sizeof( Account ) );
// replace existing record with blank record
deleteFromFile.write(
reinterpret_cast< const char * >( &blankClient ),
sizeof( Account ) );
std::cout << "Account #" << accountNumber << " deleted.\n";
} // end if
else // display error if record does not exist
cerr << "Account #" << accountNumber << " is empty.\n";
} // end deleteRecord
// display single record
void outputLine( ostream &output,Account &record )
{
output << left << setw( 10 ) << record.getAccountNumber()
<< setw( 16 ) << record.getLastName()
<< setw( 11 ) << record.getFirstName()
<< setw( 10 ) << setprecision( 2 ) << right << fixed
<< showpoint << record.getBalance() << endl;
} // end function outputLine
// obtain account-number value from user
int getAccount( const char * const prompt )
{
int accountNumber;
// obtain account-number value
do
{
std::cout << prompt << " (1 - 100): ";
std::cin >> accountNumber;
} while ( accountNumber < 1 || accountNumber > 100 );
return accountNumber;
} // end function getAccount