Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 尝试传入指向结构数组的指针时访问写入位置时发生冲突_C++_Pointers_Reference_Struct_Dynamic Arrays - Fatal编程技术网

C++ 尝试传入指向结构数组的指针时访问写入位置时发生冲突

C++ 尝试传入指向结构数组的指针时访问写入位置时发生冲突,c++,pointers,reference,struct,dynamic-arrays,C++,Pointers,Reference,Struct,Dynamic Arrays,我在试图向RealList结构添加内容的行中,在方法SetNameandOp的运行时遇到了title错误。我认为我引用RealList是错误的,但我不知道如何更正它。有人有什么想法吗 以下是headerfile中的结构: class Instructions { public: //methods struct myInstruction { string name; str

我在试图向RealList结构添加内容的行中,在方法SetNameandOp的运行时遇到了title错误。我认为我引用RealList是错误的,但我不知道如何更正它。有人有什么想法吗

以下是headerfile中的结构:

     class Instructions
    {
    public:

       //methods
        struct myInstruction
        {
            string name;
            string opcode; //opcode of add, addi etc.
            string rs; //2nd paramter
            string rt; //3rd
            string rd; //1st 
            string shamt; //shift amount
            string function; 
            myInstruction* next;
        }InstructionList;
}

class greenCard
{
public:
    //methods

    struct 
    {
        string name;
        string opcode;
        string function;
    } Instructions[29];
}
方法:

Instructions::InputtedInstructions* Instructions::get_instruction(vector<string>& vec, int counter)
{   
    InputtedInstructions* InputList = new InputtedInstructions[counter];
    myInstruction* RealList = new myInstruction[counter];
    while (ListPosition != counter)
    {

        string text = vec.at(ListPosition);
        istringstream iss(text);
        string command, arg1, arg2, arg3;

        int CommaAmount = count(text.begin(), text.end(), ',');

        if (CommaAmount == 2)
        {
            while( iss >> command >> arg1 >> arg2 >> arg3)
            {
                setNameandOp(RealList, InputList[ListPosition].name, counter);
                ListPosition++;
            }
        }
    }

    return InputList;
}   

void Instructions::setNameandOp(myInstruction* RealList, string set, int counter)
{
    greenCard NameSet;
    int y = NameSet.instructionsearch(set);
    if (y != -1)
    {
        RealList[counter].name = NameSet.Instructions[y].name;
        RealList[counter].opcode = NameSet.Instructions[y].opcode;
        RealList[counter].function = NameSet.Instructions[y].function;
    }


}

int greenCard::instructionsearch(string realinstruction)
{
    int i;
    for (i=0; i < 29; i++)
    {
        if ( realinstruction.compare(Instructions[i].name) == 0)
            return i; 
    }
    return -1;
}
指令::InputedInstructions*指令::get_指令(向量和向量,整数计数器)
{   
InputedInstructions*InputList=新的InputedInstructions[计数器];
myInstruction*RealList=新的myInstruction[计数器];
while(ListPosition!=计数器)
{
字符串文本=vec.at(ListPosition);
istringstream iss(文本);
字符串命令,arg1,arg2,arg3;
int CommaAmount=count(text.begin(),text.end(),',');
如果(CommaAmount==2)
{
而(iss>>命令>>arg1>>arg2>>arg3)
{
setNameandOp(RealList,InputList[ListPosition]。名称,计数器);
ListPosition++;
}
}
}
返回输入列表;
}   
无效指令::setNameandOp(myInstruction*RealList、字符串集、int计数器)
{
绿卡名称集;
int y=名称集指令搜索(集合);
如果(y!=-1)
{
RealList[counter]。名称=名称集。说明[y]。名称;
RealList[counter]。操作码=名称集。指令[y]。操作码;
RealList[counter]。函数=名称集。指令[y]。函数;
}
}
int绿卡::指令搜索(字符串realinstruction)
{
int i;
对于(i=0;i<29;i++)
{
if(realinstruction.compare(指令[i].name)==0)
返回i;
}
返回-1;
}

如果有什么不清楚的地方,请告诉我。如果需要的话,我非常乐意更深入地解释。我不知道还有什么可以提出的。在C++数组索引中,

从0开始,然后转到NoNoFelEnts-1。当您分配一个大小计数器数组并引用计数器th元素时,您超出了数组边界。那么您的意思是我在哪里实例化RealList只是将[counter-1]而不是counter?我觉得这不能解决问题。@cmonnats23可能不能,因为我觉得代码很混乱。但Evgeny是对的,问题是您访问的数组超出了范围。更大的问题是,代码需要重写,以便它执行您希望它执行的任何操作。您有一个名为
counter
的变量,但您没有使用它来计算任何内容,而是您分配的两个数组的大小。这就是我所说的那种混乱。柜台是从主台来的。它被传入以从main获取_指令方法。