[英]C++: calling function from vector object in main
I am trying to call printTotals()
from main()
.我正在尝试从
main()
调用printTotals()
) 。 I tried person.printTotals()
but it does not work because agent only calls functions within the vector
STL.我试过
person.printTotals()
但它不起作用,因为代理只调用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 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.我还检查了我的 STL 书,但这种情况不在其中。
I have two questions:我有两个问题:
Is there a way to call printTotals()
outside of the constructor?有没有办法在构造函数之外调用
printTotals()
? Can I call it from main()
?我可以从
main()
调用它吗? I want to keep my code clean and modular, but I am unable to access it outside of the constructor.我想保持我的代码干净和模块化,但我无法在构造函数之外访问它。
Each person
has 3 neighbors consisting of Person
s in a vector
.每个
person
有 3 个邻居,由vector
中的Person
组成。 I thought about declaring the neighbors
vector like this, but was not sure:我想过像这样声明
neighbors
向量,但不确定:
vector<Person> neighbor
For a specific person with id = 3, how can I retrieve their second neighbor's voting status?对于 id = 3 的特定人,我如何检索他们的第二个邻居的投票状态? Would it be:
可不可能是:
person[3].neighbor[1].getPersonVotingStatus()?
This is my code:这是我的代码:
Person.h人.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个人.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;
}
Try defining printTotals()
as a static function:尝试将
printTotals()
定义为 static function:
class Person
{
public:
static void printTotals(std::vector<Person> person);
}
Then you can call it in main like this:然后你可以像这样在 main 中调用它:
Person::printTotals(person);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.