奇怪的编译时错误:无法解析方法push_back

wierd compile time error: method push_back could not be resolved

本文关键字:方法 push back 编译时错误      更新时间:2023-10-16

我有以下一段代码处理向量中的向量。在使用 eclipse 时,我遇到了一个非常奇怪的编译时错误。

我正在尝试将column_info向量中现有条目的内容复制到新table_info向量中的新column_info向量。

  typedef struct _column_info
 {
char name[20]; // Column Name
int type; // 0:INT, 1: CHAR
int size;
int offset; // Start Position
 } column_info;
  typedef struct _table_info
 {
char name[20]; // Table Name
char columns[100];
vector<column_info> col;
char primary_key[20];
int recordsize;
int totalsize;
int records;
  } table_info;
  vector<table_info> v;
  table_info* get_table_info(const string& tablename)
  {
for (int i = 0; i < (int) v.size(); i++)
{
    if (strcmp(v.at(i).name, tablename.c_str()) == 0)
        return &v.at(i);
}
return NULL;
   }
  void select_table_nested(char* tablename, char* select_column[], int column_cnt[],      int nested_cnt, int select_column_count)
  {
   table_info* table_info;
   table_info = get_table_info(tablename);
   table_info new_table;
   column_info cols;
   for( int k =0; k < table_info->col.size(); k++)
   {
     strcpy(cols.name, table_info->col.at(k).name);
     cols.type = table_info->col.at(k).type;
     cols.size = table_info->col.at(k).size;
     cols.offset = table_info->col.at(k).offset;
     new_table.col.push_back(cols); ---> field 'col' could not be resolved
                                    ---> Method 'push_back' could not be resolved
    }
   }

我错过了什么吗?因为我正在同一代码的其他部分(在不同的函数中)执行push_back操作,并且没有收到此错误,除了在这个特定函数中。请帮忙。

这是第一个编译器错误吗?

   table_info* table_info;
   table_info = get_table_info(tablename);
   table_info new_table;

在第一行中,您将创建一个局部变量table_info,用于隐藏外部上下文中的类型table_info。第三行应该是编译器错误,告诉您语法错误。从那里开始,无论编译器试图解释什么,都不会让它相信new_tabletable_info 类型的对象。

你声明了一个名为 table_info 的变量,并且有一个名为 table_info 的类型,这混淆了编译器。当我通过 g++ 运行它时,它开始抱怨

table_info new_table;

因为此时table_info是一个变量名,不再是类型名。