Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.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++ CArray:无法将CArray返回的变量分配给非引用变量_C++_Data Structures_Mfc - Fatal编程技术网

C++ CArray:无法将CArray返回的变量分配给非引用变量

C++ CArray:无法将CArray返回的变量分配给非引用变量,c++,data-structures,mfc,C++,Data Structures,Mfc,我有一个包含向量的结构,如下所示: struct MY_STRUCT { LONG lVariable; CString strVariable; BOOL bVariable; vector<MY_ANOTHER_STRUCT> vecAnotherStruct; }; 有人能指出为什么第一行不行吗 编辑: 以下是我认为有助于缩小问题范围的进一步细节: 我有一个类,它包含MY_STRUCT和armystuct的定义,如下所示 class MyCl

我有一个包含向量的结构,如下所示:

struct MY_STRUCT
{
    LONG lVariable;
    CString strVariable;
    BOOL bVariable;

    vector<MY_ANOTHER_STRUCT> vecAnotherStruct;
};
有人能指出为什么第一行不行吗

编辑:

以下是我认为有助于缩小问题范围的进一步细节:

我有一个类,它包含MY_STRUCTarmystuct的定义,如下所示

class MyClass
{
    struct MY_STRUCT
    {
        LONG lVariable;
        CString strVariable;
        BOOL bVariable;

        vector<MY_ANOTHER_STRUCT> vecAnotherStruct;
    };

    CArray<MY_STRUCT> arMyStruct;

    void function()
    {
         // This line gives access violation error message 
         // when trying to access from here
         MY_STRUCT structVariable = arMyStruct[0]; 
    }
};

void someFunction()
{
    MyClass myClass;
    MyClass::MY_STRUCT aStruct;
    // initialize structure and add some data to vector

    myClass.arMyStruct.Add(aStruct);

    // This line work fine
    // when trying to access from here
    MY_STRUCT structVariable = arMyStruct[0]; 

    // When trying to access CArray element from below function, 
    // gives access violation error message
    myClass.function();

}
class-MyClass
{
结构我的结构
{
长变量;
CString标准变量;
布尔变量;
向量向量结构;
};
卡雷arMyStruct;
空函数()
{
//此行给出访问冲突错误消息
//当试图从这里访问时
MY_STRUCT structVariable=arMyStruct[0];
}
};
void函数()
{
MyClass MyClass;
MyClass::MY_STRUCT结构;
//初始化结构并向向量添加一些数据
myClass.armystuct.Add(aStruct);
//这条线很好用
//当试图从这里访问时
MY_STRUCT structVariable=arMyStruct[0];
//尝试从下面的函数访问CArray元素时,
//提供访问冲突错误消息
myClass.function();
}

第一行不起作用,因为您忽略了CArray定义的第二个参数:

CArray<MyType, MyType> myArray;
CArray-myArray;
该参数定义了(如果我没有错的话)如何访问数组元素。省略它,编译器将获得默认值:

CArray<MyType, MyType&> myArray;
CArray-myArray;
这就是为什么第一个不起作用,第二个是的原因

更新:

我已经试过你的代码了。。。如果您执行以下更正,它将起作用:

class MyClass{
public:
struct MY_ANOTHER_STRUCT
{
      float foo;
};
struct MY_STRUCT
{
    LONG lVariable;
    CString strVariable;
    BOOL bVariable;
    vector<MY_ANOTHER_STRUCT> vecAnotherStruct;
};
CArray<MY_STRUCT> arMyStruct;
void function()
{
     // This line gives access violation error message 
     // when trying to access from here
     MY_STRUCT structVariable = arMyStruct[0];
}
};


void someFunction()
{
MyClass myClass;
MyClass::MY_STRUCT aStruct;
// initialize structure and add some data to vector

myClass.arMyStruct.Add(aStruct);

// This line work fine
// when trying to access from here
MyClass::MY_STRUCT structVariable = myClass.arMyStruct[0];

// When trying to access CArray element from below function, 
// gives access violation error message
myClass.function();
}
class-MyClass{
公众:
结构我的另一个结构
{
浮福;
};
结构我的结构
{
长变量;
CString标准变量;
布尔变量;
向量向量结构;
};
卡雷arMyStruct;
空函数()
{
//此行给出访问冲突错误消息
//当试图从这里访问时
MY_STRUCT structVariable=arMyStruct[0];
}
};
void函数()
{
MyClass MyClass;
MyClass::MY_STRUCT结构;
//初始化结构并向向量添加一些数据
myClass.armystuct.Add(aStruct);
//这条线很好用
//当试图从这里访问时
MyClass::MY_STRUCT structVariable=MyClass.arMyStruct[0];
//尝试从下面的函数访问CArray元素时,
//提供访问冲突错误消息
myClass.function();
}

第一行尝试创建第一个元素的实际副本,而第二行实际上只是获取第一个元素地址的副本。所以问题可能在副本中…我尝试使用CArray myArray;得到了相同的结果“My_Other_结构”中有什么?在“My_Other_结构”中有什么:长、CString和BOOLDid的集合你是说重载CArray的运算符=吗?
CArray<MyType, MyType&> myArray;
class MyClass{
public:
struct MY_ANOTHER_STRUCT
{
      float foo;
};
struct MY_STRUCT
{
    LONG lVariable;
    CString strVariable;
    BOOL bVariable;
    vector<MY_ANOTHER_STRUCT> vecAnotherStruct;
};
CArray<MY_STRUCT> arMyStruct;
void function()
{
     // This line gives access violation error message 
     // when trying to access from here
     MY_STRUCT structVariable = arMyStruct[0];
}
};


void someFunction()
{
MyClass myClass;
MyClass::MY_STRUCT aStruct;
// initialize structure and add some data to vector

myClass.arMyStruct.Add(aStruct);

// This line work fine
// when trying to access from here
MyClass::MY_STRUCT structVariable = myClass.arMyStruct[0];

// When trying to access CArray element from below function, 
// gives access violation error message
myClass.function();
}