문제

I use operator() as a subscript operator this way:

double CVector::operator() (int i) const
{
 if (i >= 0 && i < this->size)
  return this->data[i];
 else
  return 0;
}

double& CVector::operator() (int i)
{
 return (this->data[i]);
}

It works when I get values, but I get an error when I try to write assign a value using

a(i) = 1;

UPD: Error text:

Unhandled exception at 0x651cf54a (msvcr100d.dll) in CG.exe: 0xC0000005: Access violation reading location 0xccccccc0.

도움이 되었습니까?

해결책

Like I said in my comment, the problem is your flawed design. I make a 100% guarantee on one of two things:

  1. The value you are passing to the assignment function is out of valid range.
  2. The member data is pointing to invalid space in memory.

In either case, I would suggest adding:

#include <cassert>

and adding assert(i >= 0 && i < this->size) instead of the silent failures:

double CVector::operator() (int i) const
{
    assert(i >= 0 && i < this->size);
    return this->data[i];
}

double& CVector::operator() (int i)
{
    assert(i >= 0 && i < this->size);
    return (this->data[i]);
}

다른 팁

That's because you haven't implemented error handling in double& CVector::operator() (int i) like you did for the other function which overloads ().

Change it to:

double& CVector::operator() (int i)
{
 if (i >= 0 && i < this->size)
 {
  return this->data[i];
 }
 else // Whatever manner you want to gracefully exit the program
 {
  std::cout<<"Out of bounds!"<<endl;
  exit(1);
 }
}

You should also consider changing the error handling mechanism in the other function from return 0; to something more meaningful.

이것은 닫힌 스레드 일 수 있지만 내 환경에서 동일한 오류의 이유를 공유하고 싶었을 수 있습니다.

또한 여러 서버 (농장에 새로 가입 한 커플) 오류 "전자 메일 메시지에 실패하는 데 사용되는 워크 플로만 만 사용했습니다.보낼 수 없습니다. 서버에 대한 나가는 전자 메일 설정이 올바르게 구성되어 있는지 확인하십시오 ".

이유는 2 개의 새로운 서버가 "SMTP 서버의 릴레이 서버 목록" (나는 Exchange 전문가가 아니라 엔터프라이즈 교환 팀이 아닙니다.이것을 이해할 것입니다).

릴레이 서버 목록에 2 개의 새 서버를 추가하면 오류가 사라지고 모든 것이 잘 작동하기 시작했습니다.

The problem is that you do not check for out-of-range index in your double& version of operator().

You probably cannot guarantee that data[i] points to a valid memory address for a large enough i. You should either check for out-of-range index and throw some exception or resize your vector (by allocating more memory do data) to be able to hold more values.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top