C++ 错误:'std::string'没有成员
c++ error: 'std::string' has no member
我正在创建一个目录程序,该程序提示用户输入文件名并将文件读取到字符串数组中。我的搜索名字功能有问题。 我收到一个错误:"std::string"没有名为"userRecord"的成员。我不确定如何解决这个问题,因为声明了用户记录。
页眉
#include<string>
using namespace std;
enum Title {Mr, Mrs, Ms, Dr, NA};
struct NameType {
Title title;
string firstName;
string lastName;
};
struct AddressType {
string street;
string city;
string state;
string zip;
};
struct PhoneType {
int areaCode;
int prefix;
int number;
};
struct entryType {
NameType name;
AddressType address;
PhoneType phone;
};
const int MAX_RECORDS = 50;
法典
// string bookArray[MAX_RECORDS];
entryType bookArray[MAX_RECORDS]; //Solution
int bookCount = 0;
void OpenFile(string& filename, ifstream& inData)
{
do {
cout << "Enter file name to open: ";
cin >> filename;
inData.open(filename.c_str());
if (!inData)
cout << "File not found!" << endl;
} while (!inData);
if(inData.is_open())
{
for(int i=0; i<MAX_RECORDS;i++)
{
inData>> bookArray[bookCount];
++bookCount;
}
}
}
void SearchFirstName(ifstream& inData)
{
entryType userRecord; // Declaration of userRecord
string searchName;
string normalSearchName, normalFirstName;
char choice;
bool found = false;
cout << "Enter first name to search for: ";
cin >> searchName;
for(int i = 0; i < bookCount; ++i){
normalFirstName = NormalizeString(bookArray[i].userRecord.name.firstName);
// Convert retrieved string to all uppercase
if (normalFirstName == normalSearchName) { // Requested name matches
PrintRecord(bookArray[i].userRecord.name.firstName);
cout << "Is this the correct entry? (Y/N)";
cin >> choice;
choice = toupper(choice);
cout << endl;
if (choice == 'Y') {
found = true;
break;
}
}
}
// Matching name was found before the end of the file
if (inData && !found){
cout << "Record found: " << endl;
PrintRecord(userRecord);
cout << endl;
}
else if (!found) // End of file. Name not found.
{
cout << searchName << " not found!" << endl << endl;
}
// Clear file fail state and return to beginning
inData.clear();
inData.seekg(0);
}
string bookArray[MAX_RECORDS];
bookArray
是字符串类型。它应该是
entryType bookArray[MAX_RECORDS];
也
normalFirstName = NormalizeString(bookArray[i].userRecord.name.firstName);
bookArray[i]
不能userRecord
作为成员。userRecord
是您声明的变量。它应该是
normalFirstName = NormalizeString(bookArray[i].name.firstName);
相关文章:
- 反转没有循环的字符串,C++中只有 std::string 成员
- C++ 入门 12.1:我可以在类中使用静态向量成员实现 StrBlob<string> 吗?
- 使用<string>提升::序列化序列化具有 vector<unique_ptr> 成员的类
- 如何使用<string>初始值设定项列表初始化unordered_map<字符串、unordered_set>成员?
- C++ 错误:'std::string'没有成员
- 我应该如何处理:错误 C2039 "分配":不是 Visual Studio 2017 中"cl::string"的成员?
- 'struct std::string'没有名为'c_string'的成员
- 在结构的联合成员中使用std::string时,无法引用默认构造函数
- 如何将STD :: String的值复制到Char数组类型的结构成员
- 如何将 std::vector<std::string> 转换为 boost::spirit 中结构的成员?
- std::string::substr成员函数的复杂性是什么?
- 不要使用 "const char *" 作为类的成员,如果你真的是"std::string"
- 获取指向成员std::string::size的指针无法与libc++链接,但可以与libstdc++一起使用
- 工会不会接受第 'string' 类成员
- std::string 作为成员函数的参数的奇怪行为
- 使用 move std::string 的 .data() 成员不适用于小字符串?
- 如何创建静态常量成员 std::string 数组
- 不能在没有对象的情况下调用成员函数 std::string class::function()
- 当传入具有std::string成员的结构时,C++-gdb会给出意外的输出
- 专门化模板成员函数来处理std::string和char[]参数