-1

I am trying to call printTotals() from main(). I tried person.printTotals() but it does not work because agent only calls functions within the vector STL. I checked other answers, such as C++ Calling Vectors from Function to Main and Using vector of user defined class type objects and How to create a vector of class objects in C++?.

I also checked my STL book, but this case it is not in there.

I have two questions:

  1. Is there a way to call printTotals() outside of the constructor? Can I call it from main()? I want to keep my code clean and modular, but I am unable to access it outside of the constructor.

  2. Each person has 3 neighbors consisting of Persons in a vector. I thought about declaring the neighbors vector like this, but was not sure:

    vector<Person> neighbor
    

For a specific person with id = 3, how can I retrieve their second neighbor's voting status? Would it be:

    person[3].neighbor[1].getPersonVotingStatus()?

This is my code:

Person.h

#include <iostream>
#include <string.h>
#include <vector>

class Person
{
   public: 
     Person();
     Person( std::vector<Person> person, maxPersons);
     std::string getPersonVotingStatus(int ID);
     void printTotals();

  private:
    std::string personName;
    float personHeight;
    int personID;
    std::string isVoter;
    vector<Person>neighbor;  // <--- 
}

Person.cpp

#include <iostream>
#include <string.h>
#include <vector>


Person::Person()
{}

Person::Person(int pID, float pHeight, std::string pName, std::string isV)
{
    personID = pID;
    personHeight = pHeight;
    personName = pName;
    isVoter = isV;
}

Person::Person( std::vector<Person> person, int maxPersons)
{
    for(int = 0; i < maxPersons; i++)
    {
       person[i].personID = i;
       person[i].personHeight = i + 100;
       person[i].personName = "testName";

       if (i / 2 = 0) { 
          person[i].isVoter = "yes";
       }
       else {
          person[i].isVoter = "no";
       }
   }
           Person(person[i].personID, person[i].personHeight, person[i].personName, person[i].isVoter);

}

std::String getPersonVotingStatus( int ID)
{
    return person[i].isVoter;
}

void printTotals( std::vector<Person> person)
{
   int totalVoter = 0;
   int totalNonvoter = 0;
   
   for (int i = 0; i< person.size(); i++)
      if (person[i].isVoter == "yes") {
        totalVoter++;
   }
   else {
     totalNonvoter++;
  }  
}

main.cpp

#include <iostream>
#include <string.h>
#include <vector>

int main()
{
  int maxPersons = 10;

  std::vector<Person> person;
  Person obj( person, maxPersons);

  person.printTotals(); // <--

  return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
ithoughtso
  • 103
  • 8
  • 1
    Non-static member functions act on an OBJECT, not the type. So you (in `main()`) can do `obj.printTotals()` or you can iterate over elements of `person` and call the member function of each one e.g. `for (Person &individual : person) individual.printTotals();` – Peter Nov 21 '21 at 02:45
  • Note: It doesn't work is useless information. If it worked, you wouldn't be asking the question, would you? Instead reproduce the error (cut and paste it as text into the question) or the misbehaviour you're seeing. – user4581301 Nov 21 '21 at 02:47

1 Answers1

0

Try defining printTotals() as a static function:

class Person
{
   public: 
     static void printTotals(std::vector<Person> person);
}

Then you can call it in main like this:

Person::printTotals(person);
404 Not Found
  • 3,635
  • 2
  • 28
  • 34
  • It doesn't necessarily need to be a static function, a free-standing function will work. But you should pass in the vector by reference, not by value. – Remy Lebeau Nov 21 '21 at 03:01