-
June 12th, 2017, 12:16 PM
#1
C++ Read access violation when accessing private data member
Hello, so I am trying to make a member function that returns the int processTime of an event. The class for the Event class is:
Code:
class EVENT {
private:
string device;
int type;
int processTime;
public:
EVENT(string n, int t, int pt);
void display();
int getprocessTime();
};
and the member function (called getProcessTime() plus the exception being thrown (I also put in the constructor for events) are:
Code:
EVENT::EVENT(string n, int v, int pt)
{
device = n;
type = v;
processTime = pt;
}
int EVENT::getprocessTime() {
return processTime;
}
The error:
Exception thrown: read access violation.
this was 0xCDCDCDCD.
Why is a read access violation being thrown? processTime is made in the event class, so shouldn't it be accessible outside? What should I do differently to make it be accessible by getProcessTime(); i.e add the -> operator.
Last edited by 2kaud; June 12th, 2017 at 12:57 PM.
Reason: Added code tags
-
June 12th, 2017, 12:30 PM
#2
Re: C++ Read access violation when accessing private data member
You might want to post the code causing the access violation...
Victor Nijegorodov
-
June 13th, 2017, 01:20 AM
#3
Re: C++ Read access violation when accessing private data member
Originally Posted by robin0
...
The error:
Exception thrown: read access violation.
this wasv.
Why is a read access violation being thrown?
The value 0xCDCDCDCD means that memory was allocated via malloc or new but never written by the application. See http://www.codeguru.com/cpp/w-p/win3...Management.htm
Victor Nijegorodov
-
June 20th, 2017, 02:58 AM
#4
Re: C++ Read access violation when accessing private data member
Code:
// 140.cpp
#include <iostream>
#include <string>
using namespace std;
class EVENT {
private:
string device;
int type;
int processTime;
public:
EVENT(string n, int t, int pt);
void display();
int getprocessTime();
};
EVENT::EVENT(string n, int v, int pt)
{
device = n;
type = v;
processTime = pt;
}
int EVENT::getprocessTime() {
return processTime;
}
int main()
{
EVENT ev("some string", 1, 2);
cout << ev.getprocessTime() << endl;
return 0;
}
Code:
J:\Temp\140>cl 140.cpp /EHsc
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
140.cpp
Microsoft (R) Incremental Linker Version 10.00.40219.01
Copyright (C) Microsoft Corporation. All rights reserved.
/out:140.exe
140.obj
J:\Temp\140>140.exe
2
Everything works just fine.
Last edited by Igor Vartanov; June 20th, 2017 at 03:00 AM.
Best regards,
Igor
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|