簡體   English   中英

無法理解如何將對象添加到動態數組並在 C++ 中打印它們

[英]Having trouble understanding how to add objects to a dynamic array and printing them in C++

我正在嘗試測試代碼以創建一個動態數組,該數組接受來自 phonebook2.txt 文件的不同類型的對象。 我目前無法理解為什么代碼無法運行。 在嘗試添加 printMyArray 方法后,我收到了很多錯誤消息。 任何幫助都是極好的。

我知道如何使用向量執行此操作,但我必須使用 new 運算符創建一個動態數組。

/*PATRICIA JOHNSON 973437
LINDA WILLIAMS 3532665
BARBARA BROWN 4059171
ELIZABETH JONES 2736877
JENNIFER MILLER 3863726
MARIA DAVIS 6297086
SUSAN GARCIA 6063076
MARGARET RODRIGUEZ 350662
DOROTHY WILSON 2829644
LISA MARTINEZ 6299105*/
class Contact {
    private:
        string firstName;
        string lastName;
        int phoneNumber;

    public:
        Contact(){};
        Contact(string, string, int);

        string getFirstName() {
            return firstName;
        }

        string getLastName() {
            return lastName;
        }

        int getPhoneNumber() const {
            return phoneNumber;
        }
}
#include "Contact.h"
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

void printMyArray(Contact* arr) {
    for (int i = 0; i < 50; i++) {
        cout << arr[i].getFirstName() << " " << arr[i].getLastName() << " " << arr[i].getPhoneNumber() << endl;
    }
}

int main() {
    Contact *contactsArray = new Contact[50];
    ifstream inFile("phonebook2.txt");

    int i;

    string firstName;
    string lastName;
    int pnum;

    while (inFile >> firstName >> lastName >> pnum) {
        cout << firstName << " " << lastName << " " << pnum << endl;
        Contact c(firstName, lastName, pnum);
        contactsArray[i] = c;
        i++;
    }

    for (int i = 0; i < 50; i++) {
        cout << contactsArray[i] << endl;
    }
    
    printMyArray(contactsArray);

    delete[] contactsArray;
    
    return 0;
}

Some error messages: 

main.cpp:24:5: error: 'printMyArray' was not declared in this scope
     printMyArray(contactsArray);

undefined reference to `Contact::Contact(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)'
collect2.exe: error: ld returned 1 exit status

Model 您輸入的結構:

struct Record
{
  string first_name;
  string last_name;
  string phone_number;
};

接下來,重載operator>>從輸入 stream 中讀取Record

    struct Record
    {
      string first_name;
      string last_name;
      string phone_number;
      friend std::istream& operator>>(std::istream& input, Record& r);
    };
std::istream& operator>>(std::istream& input, Record& r)
{
  input >> r.first_name;
  input >> r.last_name;
  input >> r.phone_number;
  input.ignore(1000000, '\n');
  return input;
}

將您的記錄讀入數據庫:

std::vector<Record> database;
Record r;
while (input_file >> r)
{
  database.push_back(r);
}

簡化您的生活,不要使用 static arrays 或動態 arrays。std std::vector是一個數組,會根據需要調整大小。 您不需要編寫代碼來分配數組、檢查溢出、分配新數組、將舊數組復制到新數組、刪除舊數組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM