Home > Mobile >  lab.cpp:112:14: error: no member named 'display' in 'std::vector<Invitee>'
lab.cpp:112:14: error: no member named 'display' in 'std::vector<Invitee>'

Time:09-30

#include <iostream>
#include <vector>
#include <string>
using namespace std;



  class Date {
    
    public:
       Date() {
        day = 12;
        month = 31;
        year = 2021;
      }

       Date(int x, int y, int z) {
        day = x;
        month = y;
        year = z;
      }

       void GetDate() {
         cout << "Date: " << day << "/" << month << "/" << year << endl;
        }

    private:
      int day;
      int month;
      int year;

  };

  class Time {

    public:

      Time() {
        hour = 12;
        minute = 30;
      }
      Time (int x, int y) {
        hour = x;
        minute = y;
      }
      void GetTime () {
        cout << "Time: " << hour << ":" << minute << "\n";
      }

    private:
      int hour;
      int minute;
  };

  class Invitee {
    
    string email;
    
    public:
      Invitee() {
        email = "[email protected]";
      }

      Invitee(string id) {
        if (checkId(id) == true) {
      email = id;
        }
      }

      bool checkId(string &id) {
        if (id.length() - 14 < 3) {
          cout << "Invalid Email. Please Input New Email: \n";
          cin >> id;
          checkId(id);
        }
        if (isdigit(id.at(0))) {
          cout << "Invalid Email. Please Input New Email: \n";
          cin >> id;
          checkId(id);
            };
         
        return true;
  }
      void display() {
        cout << "Email: " << email << endl;
      }
};

  class Event {
    
    string title;
    string location;
    Date *date;
    Time *time;
    vector<Invitee> list;

    public:

      Event(string t, string l, int day, int month, int year, int h, int m, string e) {
          
          title = t;
          location = l;
          date = new Date (day, month, year);
          time = new Time (h , m);
          list.push_back(e);
      }
      void display() {
        cout << "Title: " << title << endl;
        cout << "Location: " << location << endl;
        date->GetDate();
        time->GetTime();
        list.display();             ///////////////////// This Line is Issue

      }




  };


int main() {
  Event e1("Lunch", "Park", 9, 24, 2021, 12, 30, "[email protected]");

  e1.display();
  
  return 0;
}

So I understand that the compiler is getting confused between accessing the std and vector library and accessing my class Invitee. But I'm not sure exactly how to correct this issue. Can anyone help me out with this?

The problem occurs within the Class "Event" I think and within the Class Invitee under the function "Display". I am also pretty sure my display function isn't correct...

What I want it to do is display the string email that is put inside the vector<invitee> list.

Thanks In Advance!

CodePudding user response:

Compiler is complaining because you are calling vector.display(), which doesn't exist.

Instead you would want to iterate over the entries in the vector and call disaply on them

for (auto& member: list) {
    member.display();
}

CodePudding user response:

list is a vector<Invitee> and has only the member function defined by the std::vector class template, so you can't do list.display(). That member function doesn't exist.

You could however loop over the Invitees stored in list and call the display() member function on each element:

for(Invitee& inv : list) inv.display();

An unrelated suggestion: Make all the member functions that doesn't modify the object const:

void display() const {   // <- there
    cout << "Email: " << email << endl;
}

This makes it possible to call the member function in const contexts.

  •  Tags:  
  • c
  • Related