C++遍历类对象数组并调用类成员函数的实现疑问
问题:遍历Profile类对象数组并调用类函数
我希望遍历Profile类对象数组,打印每个Profile的信息。但尝试打印数组中的单个元素时(最后一行代码std::cout << online[1];)出现大量错误,其余代码运行正常。请问如何从类对象数组中调用类的特定函数?如何实现循环遍历?
#include <iostream> #include <vector> using namespace std; class Profile { public: // 成员变量定义 std::string name; int age; std::string city; std::string gender; std::vector<std::string> hobbies; std::vector<std::string> preferences; std::vector<std::string> pronouns; public: // 构造函数声明 Profile(std::string new_name, int new_age, std::string new_city, std::string new_gender); // 打印个人信息的成员函数 void view_bio() { std::cout << name << " is a " << age << " year old " << gender << ". \n"; if(pronouns.size() > 0) { std::cout << "The pronouns " << name << " uses are: \n"; for (std::string noun : pronouns) { std::cout << noun << "\n"; } } if(hobbies.size() > 0) { std::cout << "Their hobbies include:\n"; for (std::string hobby : hobbies) { std::cout << hobby << "\n"; } } } // 添加爱好的成员函数 void add_hobby(std::string new_hobby) { hobbies.push_back(new_hobby); } // 添加偏好的成员函数 void add_preference(std::string new_preference) { preferences.push_back(new_preference); } // 添加代词的成员函数 void add_pronouns(std::string new_pronouns) { pronouns.push_back(new_pronouns); } // 计算最大匹配年龄 int max_age() { int max = (age-7)*2; return max; } // 计算最小匹配年龄 int min_age() { int min = age/2+7; return min; } }; // 构造函数实现 Profile::Profile(std::string new_name, int new_age, std::string new_city, std::string new_gender){ name = new_name; age = new_age; city=new_city; gender=new_gender; }; // 比较两个Profile是否匹配的函数 bool compare(Profile date1, Profile date2) { if (!(date1.age > date2.min_age() && date1.age < date2.max_age() && date2.age > date1.min_age() && date2.age < date1.max_age() && date1.city == date2.city)){ return false; } if (date1.preferences.size() == 0 && date2.preferences.size() == 0){ return true; } else if (date1.preferences.size() > 0 && date2.preferences.size() == 0){ for (std::string pref : date1.preferences){ if(pref == date2.gender){ return true; }} return false; } else if (date2.preferences.size() > 0 && date1.preferences.size() == 0){ for (std::string pref : date2.preferences){ if(pref == date1.gender){ return true; }} return false; } else { for (std::string pref : date1.preferences){ if(pref == date2.gender){ for (std::string ferp : date2.preferences){ if(ferp == date1.gender){ return true; }}}} return false; } return false; }; int main() { Profile mags("Mags", 21, "Cincinati", "Woman"); Profile hen("Hen", 23, "Cincinati", "Man"); std::cout << compare(mags, hen); Profile online[2] = {mags, hen}; std::cout << online[1]; }
解答
1. 为什么std::cout << online[1];报错?
C++的std::cout默认仅支持内置类型(如int、string)的输出,对于自定义的Profile类对象,编译器没有默认的输出规则,因此会触发编译错误。你的类中已经实现了view_bio()方法专门用于打印对象信息,直接调用该方法即可,无需用cout直接输出对象。
2. 从类对象数组中调用特定函数的方法
数组中的每个元素都是Profile对象,通过**数组下标+成员访问运算符.**即可调用类的成员函数,格式示例:
// 调用数组第2个元素的view_bio()方法 online[1].view_bio(); // 调用max_age()函数获取最大匹配年龄 int maxAge = online[0].max_age();
3. 遍历类对象数组的几种方式
方式一:普通for循环
通过数组长度遍历每个元素:
int main() { Profile mags("Mags", 21, "Cincinati", "Woman"); Profile hen("Hen", 23, "Cincinati", "Man"); Profile online[2] = {mags, hen}; // 普通for循环遍历数组 for (int i = 0; i < 2; ++i) { online[i].view_bio(); std::cout << "-------------------\n"; // 分隔每个对象的输出 } return 0; }
方式二:范围for循环(C++11及以上版本支持)
更简洁的遍历方式,自动遍历数组中的每个对象:
int main() { Profile mags("Mags", 21, "Cincinati", "Woman"); Profile hen("Hen", 23, "Cincinati", "Man"); Profile online[2] = {mags, hen}; // 范围for循环遍历,使用引用避免对象拷贝,提升效率 for (Profile& profile : online) { profile.view_bio(); std::cout << "-------------------\n"; } return 0; }
推荐:使用std::vector代替普通数组
普通数组长度固定,灵活性差,推荐用std::vector存储Profile对象,遍历方式类似:
int main() { std::vector<Profile> online; online.emplace_back("Mags", 21, "Cincinati", "Woman"); online.emplace_back("Hen", 23, "Cincinati", "Man"); // 遍历vector for (auto& profile : online) { profile.view_bio(); std::cout << "-------------------\n"; } return 0; }
内容的提问来源于stack exchange,提问作者Maggie May
相关产品推荐
相关产品推荐

