2011-12-11 70 views
0

我使用由我的導師設置的指導編寫了該程序。一旦我終於修復了所有的拼寫錯誤和語法錯誤,我嘗試編譯該程序並恢復了5個鏈接器錯誤。據我所知,該計劃絕對沒有錯,所以我想知道你們中的任何一位是否可以指引我朝着正確的方向發展。
謝謝我收到獲取鏈接器錯誤

鏈接錯誤:

Error 2 error LNK2019: unresolved external symbol "void __cdecl write_records(class SalesRecord *)" ([email protected]@[email protected]@@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 *)" ([email protected]@[email protected]@@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> > &)" ([email protected]@[email protected]@[email protected][email protected]@[email protected]@@[email protected]@@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;   
} 
+0

我已經編輯過你的標題,因爲你的程序沒有缺陷是不對的......! –

+0

查看[本頁](http://meta.stackexchange.com/questions/10647/writing-good-titles)瞭解如何爲您的問題編寫出色的標題。 – declan

回答

0

很可能你已經忘記了這些功能的實現更多。確保你有這些功能的膽量,而不是隻聲明它們。

0

編譯器抱怨說你正在使用一些你沒有給它代碼的函數。

這些函數的定義(不是前向聲明)在您發佈的代碼中不存在。如果它們存在於其他.cpp文件中,則必須確保該文件也是您的項目的一部分(因此編譯器和鏈接器知道編譯它並使用在那裏找到的代碼)。否則,您只需編寫實現。

1

您沒有爲方法提供的定義:

write_records 
calc_discounts 
read_records 

這就是爲什麼連接抱怨。

編輯:要解決此問題,您必須在同一個文件中提供這些方法的定義,或者甚至更好地分開.hpp文件中的類聲明和.cpp文件中的類定義。