c ++ push_back不工作,因为它是应该的 [英] c++ push_back doesn't work as it is supposed

查看:245
本文介绍了c ++ push_back不工作,因为它是应该的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类symbol_table,有另一个类的对象的向量row_st.also我有一个enter方法,其中insert对象的row_st与传递的名称到所需symbol_table.but的向量中,当我调用enter输入对象使用名称:
a; b; c; Iwill将获得以下结果:a,b,c; b,c; c。向量的第一个元素获取所有输入对象的名称。第二个元素还获取后面条目的名称。

I have a class symbol_table that has a vector of objects of another class row_st.also I have an enter method where inserts objects of row_st with a passed name into the vector of desired symbol_table.but when I call the enter to enter objects with name : a;b;c;Iwill get the following result: a,b,c;b,c;c.the first element of vector gets the name of all the entered objects. and the second element also gets the name of the later entries.

  class row_st
  {
   public:
      char* name;
      type_u type;//int:0,flaot:1;char:2,bool:3,array:
      int offset;
      symbol_table *next;
      symbol_table *current;
  };
  class symbol_table
  {
   public:
    vector <row_st *> row;
     int type;
     int header;
     int starting_stmt;
     int index;
     int i;
     symbol_table *previous;
     symbol_table(){ header=0;
      previous=0; index=0;i=0;starting_stmt=0;}
  };

,这里是输入法:

 int enter(symbol_table *table,char* name,type_u type){
     row_st *t=new row_st;
t->name=name;
t->type=type;
t->offset=table->index;
t->current=table;
table->index++;
t->next=0;
table->row.push_back(t);
table->header +=1;
return table->row.size()-1;
   }

push_backed元素都指向同一个地址。

the push_backed elements all points to the same address.the new call makes the same row_st every time it is called.what should I do?

推荐答案

正如Neil Butterworth的回答所说,麻烦可能不是这个代码,但是你调用它的地方。

As Neil Butterworth's answer suggest, the trouble is probably not with this code, but the place where you call it. Using character pointers does not make it impossible to make things work, just harder.

这种情况下的问题绝对不是使用push_back。如果您发布了调用此代码的方法,则可能可以准确地查看出错的原因。

The problem in this case is definitely not with push_back. If you posted the method where you call this code it might be possible to see exactly what goes wrong.

这篇关于c ++ push_back不工作,因为它是应该的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆