C++ 不断获得;成员引用基类型';字符串[1000]';不是一个结构或联合体”;错误并且不知道如何修复它?

C++ 不断获得;成员引用基类型';字符串[1000]';不是一个结构或联合体”;错误并且不知道如何修复它?,c++,C++,此程序从用户处读取SSN,并检查它是否与提供的文本文件中的SSN匹配。尝试将数组切换为向量,但无效。尝试将数组放入structure函数并使用info::但似乎没有任何效果。我知道这是非常基本的,但我不能得到它,谢谢 #include <iostream> #include <fstream> using namespace std; int main(int argc, char* argv[]){ struct info{ string SSN;

此程序从用户处读取SSN,并检查它是否与提供的文本文件中的SSN匹配。尝试将数组切换为向量,但无效。尝试将数组放入structure函数并使用info::但似乎没有任何效果。我知道这是非常基本的,但我不能得到它,谢谢

#include <iostream>
#include <fstream>
using namespace std;

int main(int argc, char* argv[]){

  struct info{
    string SSN;
    string firstName;
    string lastName;
  };

  string list[1000];
  string userSSN;
  char x;
  fstream input(argv[1]);
  int i = 0;
  while(!input.eof()){
    input >> list.x[i] >> list.SSN[i] >> list.firstName[i] >> list.lastName[i];
    i++;
  }
  input.close();

  cout << "Input a SSN:" << endl;
  cin >> userSSN >> endl;
  for(int k = 0; k < i; k++){
    if(userSSN.compare(list.SSN[k]) == 0){
      cout << "Found at location " << k << endl;
    }
  }
}
#包括
#包括
使用名称空间std;
int main(int argc,char*argv[]){
结构信息{
字符串SSN;
字符串名;
字符串lastName;
};
字符串列表[1000];
字符串userSSN;
字符x;
fstream输入(argv[1]);
int i=0;
而(!input.eof()){
输入>>list.x[i]>>list.SSN[i]>>list.firstName[i]>>list.lastName[i];
i++;
}
input.close();
cout userSSN>>endl;
对于(int k=0;kcout
list
只是一个1000个
std::string
s的数组。看起来您需要它的类型是
info
。即使这样也不能解决您的问题,因为
info
没有名为
x
的成员。之后,访问数组中
info
的成员

list[i].SSN
不是


这段代码有太多错误,我不知道从哪里开始。让我们看看:

  • 使用
    名称空间std;
    几乎总是一个坏主意,如果您有一个标识符
    列表
    ,则情况更糟,因为它与
    std::list
    冲突

  • 不使用
    #include
    而使用
    std::string
    不能保证有效。如果包含其他标准标题,则可能有效,但不要依赖它

  • 您的变量
    x
    未使用

  • 字符串列表[1000];
    可能应该是
    信息列表[1000];

  • list.SSN[i]
    等可能应更改为
    list[i].SSN

  • list.x[i]
    毫无意义,可能会被删除。(或者您想读入未使用的
    x
    以跳过部分文件。)

  • 您无法从
    std::cin
    读入
    std::endl
    。请从该行中删除
    std::endl

  • 在这里使用
    std::string
    compare
    函数非常奇怪,只需使用
    =


  • 您代码中的问题已在其他人的回答中指出。我将向您展示一个“有效”示例:

    #包括
    #包括
    #包括
    #包括
    使用std::string;
    使用std::vector;
    使用std::cout;
    使用std::cin;
    结构信息{
    char x;//您正在从文件中读取它,所以我添加了它
    字符串SSN;
    字符串名;
    字符串lastName;
    friend std::istream&operator>>(std::istream&is,info&i){
    //也许你应该检查一下数据输入。。。
    is>>i.x>>i.SSN>>i.firstName>>i.lastName;
    回报是;
    }
    };
    int main(int argc,char*argv[]){
    //检查是否已将文件名作为参数传递
    字符串文件名;
    如果(argc<2){
    cout>文件名;
    }
    其他的
    文件名=argv[1];
    std::ifstream输入(文件名);
    如果(!输入){
    标准:cerr用户SSN;
    //这可不便宜。你可能想换一个容器或把它分类
    对于(int k=0;k你不是在
    使用名称空间std;
    并使用变量名,如
    list
    ?这个主意不好。另一个错误是在循环条件中使用
    eof
    list.SSN[i]
    
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    
    using std::string;
    using std::vector;
    using std::cout;
    using std::cin;
    
    struct info {
        char x;       // You were reading that from the file so I added it
        string SSN;
        string firstName;
        string lastName;
    
        friend std::istream &operator>>( std::istream &is, info &i ) {
            // maybe you should check data input somehow...
            is >> i.x >> i.SSN >> i.firstName >> i.lastName;
            return is;
        }
    };
    
    int main(int argc, char* argv[]) {
        // check if a file name has been passed as a parameter
        string file_name;
        if ( argc < 2 ) {
            cout << "Please, enter the input file name:\n";
            cin >> file_name;
        }
        else 
            file_name = argv[1];
    
        std::ifstream input(file_name);
        if ( !input ) {
            std::cerr << "Error. Unable to open file: " << file_name << '\n';
            exit(-1);
        }
        // Just use a vector to store all the structs
        vector<info> my_list;
        info temp_info;
        while( input >> temp_info ) {
            my_list.push_back(temp_info);
        }
        input.close();
    
        cout << "Input a SSN: ";
        string userSSN;
        cin >> userSSN;
        // That's not cheap. You may want to change the container or sort it
        for ( int k = 0; k < my_list.size(); k++ ) {
            if ( userSSN == my_list[k].SSN ) {
                cout << "Found at location " << k << '\n';
            }
        }
    
    return 0;
    }