驱动程序文件中出现错误C2440

error C2440 in driver file

本文关键字:错误 C2440 文件 驱动程序      更新时间:2023-10-16

我的驱动程序一直收到这个错误。我必须开发一个驱动程序。驱动程序将打印一个菜单,允许用户选择他们想要的选项。我以前从未创建过驱动程序,所以这是相当粗糙的。

错误:

cpp(38)错误C2440:"=":无法从"void(__cdecl*)(void)"转换为"int"

我尝试过的:

-我尝试过将变量从所有int改为所有void,反之亦然。

//banking system driver program
#include "BankingSystem.h" // Account class definition
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib> // exit function prototype
using namespace std;


void 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 )*** **-----LINE 38**
   {
      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 programn? ";
   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, balancen? ";
      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, const 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

我有第38行,试着把它和其他所有的乱七八糟的东西区分开来。。我以前从未收到过这个错误,所以我尝试修复它的选择是有限的。在把它发布到这里之前,我确实尝试过研究这个错误,但没有什么能完全解释解决方案。

我确实意识到我有从int到void和从void到int的变量。就像我说的,我试着把它们都变成这样或那样,但这一切都会导致更多的编译错误。

您可能是想调用该函数,而不仅仅是将其用作函数指针。将该行更改为:

while ( ( choice = enterChoice() ) != END )

但是声明了enterChoice以返回void,这使得该语句也没有意义。稍后,在实现中,您将其声明为返回int,因此需要使原型与之匹配。

相关文章: