2017-08-13 30 views
-1

上遇到错误undefined对“xxx”的引用,我和我的合作伙伴正在努力研究如何解决我们的代码中的错误。如果工程本科生和编程不完全是我们的专长,那么请把它放在我们身上。我继续在我的项目

对不起,如果我发送整个代码,但我很绝望,因为我们需要这个项目通过课程。谢谢。 这里是我的代码:

#include <iostream> 
#include <fstream> 
#include <stdio.h> 
#include <stdlib.h> 
#include <cctype> 
#include <iomanip> 
using namespace std; 

struct baccount { 
    int pin; 
    char accholder[50]; 
    int deposit; 
    void create_account(); 
    void display_account() const; 
    void modify(); 
    void depmoney(int); 
    void drawmoney(int); 
    void report() const; 
    int retpin() const; 
    int retdeposit() const; 
}; 

void create_account() 
{ 
    struct baccount qeqe; 
    cout << "\nEnter Pin Number:"; 
    cin >> qeqe.pin; 
    cout << "\n\nEnter your name : "; 
    cin.ignore(); 
    cin.getline(qeqe.accholder, 50); 
    do { 
     cout << "\nEnter the initial amount to be stored in the account(Minimum P2000) : "; 
     cin >> qeqe.deposit; 
    } while (qeqe.deposit < 2000); 

    cout << "\n\n\nAccount Created.."; 
}; 

void display_account() 
{ 
    struct baccount qeqe; 
    cout << "\nPin Number: " << qeqe.pin; 
    cout << "\nAccount Holder Name : "; 
    cout << qeqe.accholder; 
    cout << "\nCurrent Balance : " << qeqe.deposit; 
} 

void depmoney(int x) 
{ 
    struct baccount qeqe; 
    qeqe.deposit += x; 
} 

void modify() 
{ 
    struct baccount qeqe; 
    cout << "\nPin Number : " << qeqe.pin; 
    cout << "\nModify Account Holder Name : "; 
    cin.ignore(); 
    cin.getline(qeqe.accholder, 50); 
    cout << "\nModify Balance amount : "; 
    cin >> qeqe.deposit; 
} 

void drawmoney(int x) 
{ 
    struct baccount qeqe; 
    qeqe.deposit -= x; 
} 

void report() 
{ 
    struct baccount qeqe; 
    cout << qeqe.pin << setw(15) << " " << qeqe.accholder << setw(15) << " " << setw(8) << qeqe.deposit << endl; 
} 

int retpin() 
{ 
    struct baccount qeqe; 
    return qeqe.pin; 
} 

int retdeposit() 
{ 
    struct baccount qeqe; 
    return qeqe.deposit; 
} 

void write_account(); 
void display_sp(int); 
void modify_account(int); 
void delete_account(int); 
void display_all(); 
void deposit_withdraw(int, int); 

int main() 
{ 
    char choice; 
    int pnum; 
    do { 
     system("cls"); 
     cout << "Welcome to Cutaran-Eleria Bank" << endl; 
     cout << "[1] Create new account" << endl; 
     cout << "[2] Deposit" << endl; 
     cout << "[3] Withraw" << endl; 
     cout << "[4] Show current balance" << endl; 
     cout << "[5] Check list of accounts" << endl; 
     cout << "[6] Remove an account" << endl; 
     cout << "[7] Edit an account" << endl; 
     cout << "[8] Exit Cutaran-Eleria Bank" << endl; 
     cout << "Please enter the command you want to do: " << endl; 
     cin >> choice; 
     system("cls"); 
     switch (choice) { 
     case '1': 
      write_account(); 
      break; 
     case '2': 
      cout << "\n\n\tEnter Pin Number: "; 
      cin >> pnum; 
      deposit_withdraw(pnum, 1); 
      break; 
     case '3': 
      cout << "\n\n\tEnter The account No. : "; 
      cin >> pnum; 
      deposit_withdraw(pnum, 2); 
      break; 
     case '4': 
      cout << "\n\n\tEnter The account No. : "; 
      cin >> pnum; 
      display_sp(pnum); 
      break; 
     case '5': 
      display_all(); 
      break; 
     case '6': 
      cout << "\n\n\tEnter Pin Number : "; 
      cin >> pnum; 
      delete_account(pnum); 
      break; 
     case '7': 
      cout << "\n\n\tEnter Pin Number. : "; 
      cin >> pnum; 
      modify_account(pnum); 
      break; 
     case '8': 
      cout << "\n\nThank you for using Cutaran-Eleria Bank"; 
      break; 
     default: 
      cout << "\a"; 
     } 
     cin.ignore(); 
     cin.get(); 
    } while (choice != '8'); 
    return 0; 
} 

void write_account() 
{ 
    struct baccount qeqe; 
    ofstream newrecord; 
    newrecord.open("accounts.dat", ios::binary | ios::app); 
    qeqe.create_account(); 
    newrecord.write(reinterpret_cast<char*>(&qeqe), sizeof(baccount)); 
    newrecord.close(); 
    cout << "Your account is now saved into the system."; 
} 

void display_sp(int n) 
{ 
    baccount bankacc; 
    bool flag = false; 
    ifstream oldrecords; 
    oldrecords.open("accounts.dat", ios::binary); 
    if (!oldrecords) { 
     cout << "The account is not in the system"; 
     return; 
    } 
    cout << "\nCurrent Balance\n"; 

    while (oldrecords.read(reinterpret_cast<char*>(&bankacc), sizeof(baccount))) { 
     if (bankacc.retpin() == n) { 
      bankacc.display_account(); 
      flag = true; 
     } 
    } 
    oldrecords.close(); 
} 

void modify_account(int n) 
{ 
    bool found = false; 
    baccount acc; 
    fstream File; 
    File.open("accounts.dat", ios::binary | ios::in | ios::out); 
    if (!File) { 
     cout << "Unable to open account."; 
     return; 
    } 
    while (!File.eof() && found == false) { 
     File.read(reinterpret_cast<char*>(&acc), sizeof(baccount)); 
     if (acc.retpin() == n) { 
      acc.display_account(); 
      cout << "\n\nEnter The New Details of account" << endl; 
      acc.modify(); 
      int pos = (-1) * static_cast<int>(sizeof(baccount)); 
      File.seekp(pos, ios::cur); 
      File.write(reinterpret_cast<char*>(&acc), sizeof(baccount)); 
      cout << "\n\n\t Record Updated"; 
      found = true; 
     } 
    } 
    File.close(); 
    if (found == false) 
     cout << "\n\n Record Not Found "; 
} 

void delete_account(int n) 
{ 
    baccount acc; 
    ifstream oldrecords; 
    ofstream newrecord; 
    oldrecords.open("accounts.dat", ios::binary); 
    if (!oldrecords) { 
     cout << "File could not be opened!!! Press any Key..."; 
     return; 
    } 
    newrecord.open("Temp.dat", ios::binary); 
    oldrecords.seekg(0, ios::beg); 
    while (oldrecords.read(reinterpret_cast<char*>(&acc), sizeof(baccount))) { 
     if (acc.retpin() != n) { 
      newrecord.write(reinterpret_cast<char*>(&acc), sizeof(baccount)); 
     } 
    } 
    oldrecords.close(); 
    newrecord.close(); 
    remove("accounts.dat"); 
    rename("Temp.dat", "accounts.dat"); 
    cout << "\n\nRecord Deleted .."; 
} 

void display_all() 
{ 
    baccount acc; 
    ifstream oldrecords; 
    oldrecords.open("accounts.dat", ios::binary); 
    if (!oldrecords) { 
     cout << "Unable to show accounts"; 
     return; 
    } 
    cout << "\n\n\t\tList of Accounts\n\n"; 

    cout << "Pin Number  Name  CurrentBalance\n"; 

    while (oldrecords.read(reinterpret_cast<char*>(&acc), sizeof(baccount))) { 
     acc.report(); 
    } 
    oldrecords.close(); 
} 

void deposit_withdraw(int n, int option) 
{ 
    int amt; 
    bool found = false; 
    baccount acc; 
    fstream File; 
    File.open("accounts.dat", ios::binary | ios::in | ios::out); 
    if (!File) { 
     cout << "Unable to access the account."; 
     return; 
    } 
    while (!File.eof() && found == false) { 
     File.read(reinterpret_cast<char*>(&acc), sizeof(baccount)); 
     if (acc.retpin() == n) { 
      acc.display_account(); 
      if (option == 1) { 
       cout << "\n\nEnter The amount to be deposited"; 
       cin >> amt; 
       acc.depmoney(amt); 
      } 
      if (option == 2) { 
       cout << "\n\nEnter The amount to be withdrawn"; 
       cin >> amt; 
       int bal = acc.retdeposit() - amt; 
       if (amt > acc.retdeposit()) 
        cout << "Insufficient balance"; 
       else 
        acc.drawmoney(amt); 
      } 
      int pos = (-1) * static_cast<int>(sizeof(acc)); 
      File.seekp(pos, ios::cur); 
      File.write(reinterpret_cast<char*>(&acc), sizeof(baccount)); 
      cout << "\n\nRecord Updated"; 
      found = true; 
     } 
    } 
    File.close(); 
    if (found == false) 
     cout << "\n\n Record Not Found "; 
} 
+0

你得到的* exact *错误信息是什么? (也就是说哪些名字是未定义的?) – NPE

+1

等等,你知道'retpin'&'retdeposit'会返回垃圾值,因为你从每次调用这些函数时创建的未初始化的'struct baccount qeqe'返回数据吗? – ForceBru

+0

另外,'create_account'就像没有任何方式绑定到结构一样无用。你应该把它叫做'baccount :: create_account'。该结构的作用与一个类相同。 – ForceBru

回答

1

你得到未定义参考 becouse你还没有函数的定义(在这种情况下)。

这意味着链接程序找不到函数的(您正在尝试调用)的定义。

例如,你所要做的,就是:

结构

之外
void baccount::display_account() const 
{ 
    std::cout << "Now you can call me!"; 
} 

或右的结构

struct baccount 
{ 
    int pin; 
    void display_account() const 
    { 
     std::cout << "Now you can call me!"; 
    } 
    . 
    . 
    . 
}; 
0

“未定义的参考... “是一个链接器错误,不是编译器错误。这意味着你忘记了链接一些东西(库,目标文件),它定义了错误所抱怨的任何类型。 将缺少的对象文件或库添加到链接器命令行中,您应该很好。

请注意,根据工具链,您链接事物的订单可能很重要。始终对链接进行排序,以便在实现它们的对象之前提及取决于类型的对象。

相关问题