C++症状录入程序报错:无法将void转换为std::string求助
Hey there! Let's break down your issues step by step and fix up that code for you.
First, the error you're hitting:
error: could not convert ‘symptom.std::vector...push_back(...)’ from ‘void’ to ‘std::string’
This happens because std::vector::push_back() returns nothing (its return type is void), but your disease function declares that it returns a std::string. You're trying to return the result of push_back, which doesn't make sense—push_back just adds an element to the vector and calls it a day.
Beyond the compile error, there are a few critical logic/runtime problems:
- Vector Out-of-Bounds Access: When you call
disease(dry[patientn], namex[patientn], patientn), thedryvector is empty at first. Accessingdry[0]when the vector has no elements is undefined behavior (your program might crash or behave unpredictably). - Pass-by-Value Means No Permanent Changes: Your
diseasefunction takesvector<string> symptomas a parameter—this creates a copy of the original vector. Any changes you make tosymptominside the function won't affect the originaldryorsneezevectors inmain. - Unfinished Function Logic: If the user enters something other than Y/N, your function doesn't return a value, which is another undefined behavior.
- Poor Data Structure Choice: Having separate vectors for each symptom (
dry,sneeze) makes your code hard to maintain—adding a new symptom later would require creating a whole new vector and updating every part of your code that handles patients.
Here's a revised version that fixes all these issues and makes your code cleaner:
#include <iostream> #include <vector> #include <string> // Helper function to get valid Y/N input for a symptom bool getValidSymptomResponse(const std::string& patientName, const std::string& symptom, std::string& result) { std::string input; std::cout << "Is " << patientName << " experiencing " << symptom << "? (Y/N): "; std::cin >> input; if (input == "Y" || input == "y") { result = "Y"; return true; } else if (input == "N" || input == "n") { result = "N"; return true; } else { std::cout << "Oops! Please enter only Y or N.\n"; return false; } } // Struct to group all data for a single patient struct Patient { std::string name; std::string dryCough; std::string sneezing; }; int main() { std::vector<Patient> patients; const int totalPatients = 2; for (int i = 0; i < totalPatients; ) { Patient newPatient; std::cout << "Enter patient name: "; std::cin >> newPatient.name; // Keep asking until we get valid input for each symptom while (!getValidSymptomResponse(newPatient.name, "dry cough", newPatient.dryCough)); while (!getValidSymptomResponse(newPatient.name, "sneezing", newPatient.sneezing)); patients.push_back(newPatient); i++; } // Print out all patient data neatly std::cout << "\n--- Patient Symptom Summary ---\n"; for (const auto& patient : patients) { std::cout << "Patient: " << patient.name << "\n"; std::cout << "- Dry Cough: " << patient.dryCough << "\n"; std::cout << "- Sneezing: " << patient.sneezing << "\n"; std::cout << "------------------------------\n"; } return 0; }
- Use a Struct for Patient Data: Grouping a patient's name and symptoms into a
Patientstruct makes your code far more readable and scalable—adding a new symptom (like "fever") just means adding a new field to the struct, not a whole new vector. - Pass Parameters by Reference: Using
const std::string&for read-only data avoids unnecessary copies, andstd::string&lets us modify the original value directly without copying vectors. - Input Validation Loops: Instead of exiting when the user enters bad input, we loop until they enter valid Y/N—this is much more user-friendly.
- Avoid
using namespace std: While it's common in small programs, it can cause naming conflicts in larger projects. Usingstd::explicitly makes your code clearer. - Cleaner Output: Adding separators and clear labels makes the final summary easier to read.
内容的提问来源于stack exchange,提问作者Franklin Fredric

