0

我使用我的导师制定的指导方针编写了这个程序。一旦我最终修复了所有的拼写错误和语法错误,我就尝试编译程序并恢复了 5 个链接器错误。据我所知,该程序绝对没有任何问题,所以我想知道你们中是否有人能指出我正确的方向。
谢谢

我收到的链接器错误:

Error   2 error LNK2019: unresolved external symbol "void __cdecl write_records(class SalesRecord *)" (?write_records@@YAXPAVSalesRecord@@@Z) referenced in function _main  C:\Users\Home\Documents\Visual Studio 2010\Projects\Assignment10\Assignment10\Assign10.obj

Error   3 error LNK2019: unresolved external symbol "void __cdecl calc_discounts(class SalesRecord *)" (?calc_discounts@@YAXPAVSalesRecord@@@Z) referenced in function _main    C:\Users\Home\Documents\Visual Studio 2010\Projects\Assignment10\Assignment10\Assign10.obj

Error   4 error LNK2019: unresolved external symbol "class SalesRecord * __cdecl read_records(class std::basic_ifstream<char,struct std::char_traits<char> > &)" (?read_records@@YAPAVSalesRecord@@AAV?$basic_ifstream@DU?$char_traits@D@std@@@std@@@Z) referenced in function _main    C:\Users\Home\Documents\Visual Studio 2010\Projects\Assignment10\Assignment10\Assign10.obj

Error   5 error LNK1120: 3 unresolved externals C:\Users\Home\Documents\Visual Studio 2010\Projects\Assignment10\Debug\Assignment10.exe 1
//Author William Lovejoy
//Assignment 10

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <string>
using namespace std;

const int MAX_FILE_NAME   = 35;
const int MAX_ID_LEN      = 10; 
const int MAX_NAME_LEN    = 30; 
const double DISCOUNT = 0.10;
const double DISCOUNT_BREAK_POINT = 10;


class SalesRecord;
typedef SalesRecord * SalesRecordPtr;

class SalesRecord
{ private:
        char item_id[MAX_ID_LEN + 1];
    char item_name[MAX_NAME_LEN + 1];
    int quantity_sold;
    double regular_price;
    double discount_price;
    double total_price;
    SalesRecord *next;
  public:
    SalesRecord();
    void read(ifstream& in);
    void calc_discount_price();
    void write(ostream & os) const;
    int quantity();
    double total_for_item();
    bool operator<(const SalesRecord& right) const;

        friend SalesRecordPtr read_records (ifstream& in);
    friend void calc_discounts(SalesRecordPtr head);
    friend void write_records(SalesRecordPtr head);
    friend void append(SalesRecordPtr& head, SalesRecord& thisrecord);
    friend void delete_records(SalesRecordPtr& head);

};

void open_input(ifstream& input, char name[]);
void open_output(ofstream& output, char name[]);

int main()
{ char again;
int num_records;
char infilename[MAX_FILE_NAME + 1];
ifstream in;
SalesRecordPtr records = NULL;

do 
{ open_input(in, infilename);
  records = read_records(in);
  in.close();                             
  if (records != NULL)
  {  calc_discounts(records);    
     write_records(records);  
     delete_records(records);                    
  }
  else
  {  cout << "\n\n\aNo data in file: " << infilename << endl;
  }

  cout << "\nDo you want to process another file (Y/N)? ";
  cin >> again;
  cin.ignore(1, '\n'); 
  } 
  while ( again == 'y' || again == 'Y'); 

  cout <<  "\n\n***** END OF PROGRAM ******\n";
  return 0;
}  

void open_input(ifstream& input, char name[]) 
{  int count = 0;             
   do
   {  count++;
      if (count != 1)  
      {  cout << "\n\aInvalid file name or file does not exist. Please try again." 
              << endl;
      }

      cout << "\nEnter the input file name (maximum of " << MAX_FILE_NAME
           << " characters please)\n:> ";
      cin.get(name, MAX_FILE_NAME + 1);
      cin.ignore(81, '\n');           
      input.clear();                   
   } while (input.fail() );            
}

void open_output(ofstream& output, char name[]) 
{  int count = 0;           
   do 
   {  count++;
      if (count != 1)  
      {  cout << "\n\aInvalid file name or file does not exist. Please try again." 
              << endl;
      }

      cout << "\nEnter the input file name (maximum of " << MAX_FILE_NAME
           << " characters please)\n:> ";
      cin.get(name, MAX_FILE_NAME + 1);
      cin.ignore(81, '\n');           
      output.clear();                  
      output.open(name); 
   } while (output.fail() );            
} 

bool SalesRecord::operator<(const SalesRecord& right) const

{  if (_stricmp(item_name, right.item_name) < 0)  return true;
   else                                        return false;
} 

SalesRecord::SalesRecord()
{   next = NULL;
}

void SalesRecord::read(ifstream& in)      

{  in.get(item_id, MAX_ID_LEN +1);       
   while (in.get() != '\n');              

   in.get(item_name, MAX_NAME_LEN +1);    
   while (in.get() != '\n');             

   in >> quantity_sold >> regular_price;  
   while (in.get() != '\n');              

} 

void SalesRecord::calc_discount_price()      
{  double discount_rate;

   if (quantity_sold < DISCOUNT_BREAK_POINT)
      discount_rate = 0.0;
   else
      discount_rate = DISCOUNT;

   discount_price = regular_price - (discount_rate * regular_price);
   total_price = quantity_sold * discount_price;
}

void SalesRecord::write(ostream & os) const                                                    
{  os.setf(ios::fixed);   os.setf(ios::showpoint);   os.precision(2);

   os << item_id << "\n" << item_name << "\n"
       << quantity_sold << " " << discount_price << " "
       << total_price << endl;
}    

void append(SalesRecordPtr& head, SalesRecord& thisrecord) 

{  SalesRecord *    new_record = NULL; 
   SalesRecord *    last       = NULL;

   new_record = new SalesRecord;      
   if (new_record == NULL)
   {   cout << "\aCan not allocate memory!";
       exit(1);
   }

   *new_record = thisrecord;                  
   new_record->next = NULL;           

   if (head == NULL)              
   {   head = new_record;
   }
   else                               
   {   last = head;                 
       while ( last->next != NULL)    
       {   last = last->next;         
       }

       last->next = new_record;       
   }
}

void delete_records(SalesRecordPtr& head)                                            
{  SalesRecord *  current  = NULL; 
   SalesRecord *  deadmeat = NULL; 

   current = head;              
   while (current != NULL)        
   {   deadmeat = current;          
       current = current->next; 
       delete deadmeat;           
   }

   head = NULL;         
} 
4

3 回答 3

1

您尚未提供方法的定义:

write_records
calc_discounts
read_records

这就是链接器抱怨的原因。

编辑:要解决该问题,您必须在同一文件中为这些方法提供定义,或者更好地将 .hpp 文件中的类声明和 .cpp 文件中的类定义分开。

于 2011-12-11T02:51:24.987 回答
0

您很可能已经忘记了这些功能的实现。确保你有这些功能的胆量,而不是仅仅声明它们。

于 2011-12-11T02:45:02.917 回答
0

编译器抱怨你正在使用一些你没有给它代码的函数。

您发布的代码中不存在这些函数的定义(不是前向声明)。如果它们存在于其他 .cpp 文件中,您必须确保该文件也是您项目的一部分(因此编译器和链接器知道编译它并使用那里找到的代码)。否则,您只需要编写实现。

于 2011-12-11T02:45:26.260 回答