C++ 如何在c++;?

C++ 如何在c++;?,c++,class,dynamic-arrays,C++,Class,Dynamic Arrays,为动态对象数组设置一个值被证明是很棘手的。我正试图用一种经典的方法来做这件事 void Function(CallsOfTheObject * r){ const char DELIM = '>' ; ifstream file("file.txt"); string sTemp; int ri = 1; while(getline(file, sTemp)) { if(!sTemp.empty() && sTemp[0]==DELIM){

为动态对象数组设置一个值被证明是很棘手的。我正试图用一种经典的方法来做这件事

void Function(CallsOfTheObject * r){
  const char DELIM = '>' ;
  ifstream file("file.txt"); 
  string sTemp; 
  int ri = 1;

  while(getline(file, sTemp)) {
    if(!sTemp.empty() && sTemp[0]==DELIM){
        Expand(r, ri);
        r[ri].setName(sTemp.substr(1)); //The line resulting with error
以及其他使用的功能和方法:

void Expand(CallsOfTheObject *r, int ri){
  CallsOfTheObject* newR = new CallsOfTheObject[ri];
  for (int i = 0; i < ri; i++){
      if(r[i].getName() != ""){
         newR[i] = r[i];
      }else{
          newR[i] = CallsOfTheObject();
      }
  }
  delete []r;
  r = newR;
}
我不断地发现这个错误:

System.Runtime.InteropServices.SEHException was unhandled
Message: External component has thrown an exception.
我的猜测是,我没有在某处释放内存,或者犯了一个很小的错误,比如打字错误

编辑:我向扩展函数添加了指针符号,但唯一更改的是错误:

System.AccessViolationException was unhandled
Message: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
看起来我没有清除一些内存,或者试图以不合适的方式覆盖现有的内存。至少我发布的代码看起来很干净。。。对我来说

编辑2:我正在查看我的调用堆栈,但它没有告诉我多少。你能告诉我那是什么意思吗

    S2_L2.exe!std::char_traits<char>::copy(char* _First1, char* _First2, unsigned int _Count) Line 497  C++
    S2_L2.exe!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::assign(std::basic_string<char,std::char_traits<char>,std::allocator<char> >& _Right, unsigned int _Roff, unsigned int _Count) Line 904 + 0x28 bytes C++
    S2_L2.exe!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::assign(std::basic_string<char,std::char_traits<char>,std::allocator<char> >& _Right) Line 888 + 0x13 bytes  C++
    S2_L2.exe!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::operator=(std::basic_string<char,std::char_traits<char>,std::allocator<char> >& _Right) Line 764 + 0xc bytes    C++
    S2_L2.exe!Recipe::setName(std::basic_string<char,std::char_traits<char>,std::allocator<char> >* a) Line 22 + 0xb bytes  C++
    S2_L2.exe!InputR(Recipe&& r) Line 146   C++
>   S2_L2.exe!main(array<System::String^> ^ args) Line 120  C++
S2_L2.exe!STD::CARY: 497:C++(CAR**FILST1,CHAR**FiSTST2,无符号int Syt)
S2_L2.exe!STD::Basic SqrS::赋值(STD:Stase:Basic Syth&YeLoad,未签名int yRoFF,未签名int xCalt)904 +0x28字节C++
S2_L2.exe!STD::Basic字符串::赋值(STD::Basic字符串和右)行888 +0x13字节C++
S2_L2.exe!STD::Basic SqcSn::操作符=(STD::Basic字符串和右)行764 +0xC字节C++
S2_L2.exe!秘诀::StNeNT(STD::Basic字符串S*A)行22 +0xB字节C++
S2_L2.exe!InputR(配方&)146线C++
>S2_L2.exe!主(数组^ ARGS)120行C++

Expand
按值接收指针
r
,然后继续更改它。除非通过引用传递指针(
CallsOfTheObject*&r
),否则调用者无法看到这些更改。另外,如果
CallsOfTheObject
似乎是一个动态数组,请改用
std:vector
。这既简单又安全。在调用扩展之前,“r”在哪里分配?我希望我可以使用向量!不幸的是,我必须生成一个可行的动态数组样本……在我看来,在将其发送到扩展之前,您并没有初始化r。为什么不能使用向量?我确实初始化了它的主函数(在其中调用函数(CallsOfTheObject*r=newcallsoftheobject[0];)的函数)。我至少不能使用动态数组一次,因为这是一项家庭作业,旨在检查我们对动态数组的理解程度。
    S2_L2.exe!std::char_traits<char>::copy(char* _First1, char* _First2, unsigned int _Count) Line 497  C++
    S2_L2.exe!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::assign(std::basic_string<char,std::char_traits<char>,std::allocator<char> >& _Right, unsigned int _Roff, unsigned int _Count) Line 904 + 0x28 bytes C++
    S2_L2.exe!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::assign(std::basic_string<char,std::char_traits<char>,std::allocator<char> >& _Right) Line 888 + 0x13 bytes  C++
    S2_L2.exe!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::operator=(std::basic_string<char,std::char_traits<char>,std::allocator<char> >& _Right) Line 764 + 0xc bytes    C++
    S2_L2.exe!Recipe::setName(std::basic_string<char,std::char_traits<char>,std::allocator<char> >* a) Line 22 + 0xb bytes  C++
    S2_L2.exe!InputR(Recipe&& r) Line 146   C++
>   S2_L2.exe!main(array<System::String^> ^ args) Line 120  C++