Print()函数无法输出向量,什么错误?

时间:2015-11-20 22:50:53

标签: c++ function vector

所以我的工作是描述我们的老师只给我们原型的电话簿作业。他还给我们建了一个main(),我们不允许改变他的main()或原型,只描述它们。我正在处理void print()函数,即靠近底部的函数。 cout括号<<被标记为错误。如果你们对void erase()函数有任何提示,我会在void lookup()内使用erase()函数,但问题是我应该删除index和lookup函数返回电话号码,而不是索引。 首先列出的是原型,然后是我的工作,然后是主要的。

PROTOYPES

    #ifndef PHONEBOOK_H
    #define PHONEBOOK_H
    #include <string>
    #include <vector>
    using namespace std;

    class Person
    {
    public:
        Person();
        Person(string new_name, int new_phone);
        string get_name() const;
        int get_phone() const;
        bool operator < (Person p) const;
        void print() const;

    private:
        string name;
        int phone;
    };

    void add_people(vector<Person> &phone_book);
    void erase(vector<Person> &phone_book, string name);
    void sort(vector<Person> &phone_book);
    void shuffle(vector<Person> &phone_book);
    void reverse(vector<Person> &phone_book);
    void print(vector<Person> &phone_book);
    int lookup(const vector<Person> &phone_book, string name);
    #endif

我的工作

        #include <iostream>
        #include <string>
        #include <vector>
        #include <time.h>
        #include "phonebook.h"

        Person::Person()
        {
            name = "NONE";
            phone = 0000000;

        }
        Person::Person(string new_name, int new_phone)
        {
            name=new_name;
            phone=new_phone;
        }
        string Person::get_name() const
        {
            return name;
        }
        int Person::get_phone() const
        {
            return phone;
        }
        bool Person::operator < (Person p) const
        {
            return name < p.name;
        }
        void Person::print() const
        {
            cout << endl << name << " " << phone;
        }

        void add_people(vector<Person> &phone_book)
        {
            cout << "Please enter the new name: ";
            string s;
            getline(cin, s);
            cout << "Please enter new number: ";
            int number;
            cin >> number;
            phone_book.push_back(Person(s,number));
        }
        void erase(vector<Person> &phone_book, string name)
        {   
        }
        void sort(vector<Person> &phone_book)
        {
        }
        void shuffle(vector<Person> &phone_book)
        {
        }
        void reverse(vector<Person> &phone_book)
        {
        }
        void print(vector<Person> &phone_book)
        {
            for(int i=0; i < phone_book.size(); i++)
                cout << phone_book[i] << " ";
        }

        int lookup(const vector<Person> &phone_book, string name)
        {
                int i = 0;
                while (i < phone_book.size() && phone_book[i].get_name() != name)
                {
                    i++;
                    return phone_book[i].get_phone();
                }
        }

主要

#include <iostream>
#include <string>
#include <vector>
#include <time.h>
#include "phonebook.h"

using namespace std;




int main()
{

    vector<Person> phone_book;
    string name;
    int number;
    int answer;

    srand((int)(time(0)));

    phone_book.push_back(Person("Bruin, Joe", 5556456));
    phone_book.push_back(Person("Simpson, Homer", 5557471));
    phone_book.push_back(Person("Duffman, Barry", 5533331));


    cout <<"\n";
    cout << "Your phone book contains the following names and numbers: \n";

    for (int i=0; i < phone_book.size(); i++)
    {

        phone_book[i].print();
        cout << "\n";
    }
    cout <<"\n";
    answer=0;

    while (answer != 8)
    {
        cout << "\nChoose from the following options:\n\n";

        cout << "1) Add people to the phone book.\n";
        cout << "2) Erase a person from the phone book.\n";
        cout << "3) Sort the phone book.\n";
        cout << "4) Shuffle the phone book.\n";
        cout << "5) Reverse the phone book.\n";
        cout << "6) Print the phone book.\n";
        cout << "7) Look up a person in the phone book.\n";
        cout << "8) Quit.\n\n";

        cin >> answer;

        string clear;

        getline(cin, clear);

        if (answer == 1)
            add_people(phone_book);
        else if (answer == 2)
        {
            cout << "Enter a name: ";
            getline(cin, name);
            erase(phone_book, name);

        }
        else if (answer == 3)
            sort(phone_book);
        else if (answer == 4)
            shuffle (phone_book);
        else if (answer == 5)
            reverse(phone_book);
        else if (answer == 6)
        {
            cout <<"\n";
            cout << "Your phone book contains the following names and numbers: \n";
            print(phone_book);
        }
        else if (answer ==7)
        {                   
            cout << "Enter a name: ";
            getline(cin, name);
            int number = lookup(phone_book, name); 
            if (number > 0)
            {
                cout << "\n\nThe number for " << name << " is: " << number << "\n\n";
            }
            else
                cout << name << " not found in the phone book.\n";
        }
    }
    return 0;
}

2 个答案:

答案 0 :(得分:1)

cout << phone_book[i] << " ";

是一个问题,因为operator<<cout之间未定义Person。您可以通过定义此类函数或在循环中调用Person::print来解决问题。

void print(vector<Person> &phone_book)
{
    for(int i=0; i < phone_book.size(); i++)
    {
        phone_book[i].print();
        cout << " ";
    }
}

建议改进

不要在课堂的任何地方假设coutcin。无论您在类实现中使用哪一个,都要将函数更改为使用std::ostream&std::istream&作为输入参数。然后,您可以在调用函数时选择使用任何std::ostreamstd::istream。您可以决定从文件中读取数据,或将数据写入文件。在main中做出决定,让课程免于该决定。

例如,将Person::print()更改为:

void print(std::ostream& out) const;

并实施:

void Person::print(std::ostream& out) const
{
    out << endl << name << " " << phone;
}

将非成员函数print更改为:

void print(vector<Person> &phone_book, std::ostream& out)
{
    for(int i=0; i < phone_book.size(); i++)
    {
        phone_book[i].print(out);
        out << " ";
    }
}

答案 1 :(得分:0)

cout << phone_book[i] << " ";

这条线没有意义。 phone_book是人物的载体,那么你如何期望cout打印整个人?!要解决这个问题,你必须告诉它你想要打印出来的人的哪个成员变量。不幸的是,以下代码无法工作:

cout << phone_book[i].name << " - " << phone_book[i].phone << endl;

因为成员变量namephone已经成为private(无法在类本身之外访问它们)。幸运的是,你碰巧在Person类中已经有了一个为你打印它的函数:

void Person::print() const
{
    cout << endl << name << " " << phone;
}

如果您拨打此功能,则应打印出该人的姓名和电话号码。要打电话,只需使用:

for(int i=0; i < phone_book.size(); i++)
    phone_book[i].print();

另一种方法是重载 <<运算符 - 这基本上意味着重新定义在类上使用<<意味着什么。例如,您可以基于整数创建新的数据类型,但会使+运算符重载,以便减去而不是添加。我将给你一些示例代码:

class myVar
{
    public:
        int value;

    myVar operator+(myVar varToSubtract)
    {
        return value - varToSubtract;
    }
};

同样的原则适用于<< - 我确定你可以自己弄清楚其余部分:)