Discussion:
problem declaring stl vector in header
(too old to reply)
Roger
2008-05-09 13:50:19 UTC
Permalink
I have a simple test running on BCB 6 in a Console Application that
gives a Debugger Exception Notification of a raised exception
EAccessViolation when I try to do, say, vInt.resize(1) in the cpp file
as shown below. I would appreciate any help explaining what I am doing
wrong.

Thanks,
Roger

MyUtilities.h File:

#ifndef MyUtilitiesH
#define MyUtilitiesH

#include <vector>

class TUtilities
{
private:

protected:

public:
bool __fastcall TUtilities::Test();

std::vector<int> vInt;
};

extern TUtilities *MyUtils;
#endif


MyUtilities.cpp file:

#include "MyUtilities.h"


TUtilities *MyUtils;

bool __fastcall TUtilities::Test()
{
vInt.resize(1); // When I run it I get an Access Violation error
return true;
}
Thomas Maeder [TeamB]
2008-05-09 15:06:35 UTC
Permalink
[Please next time choose a more appropriate Subject: header; AFAICT,
your problem is not with declaring a vector, but resizing
one. Thanks!]
Post by Roger
I have a simple test running on BCB 6 in a Console Application that
gives a Debugger Exception Notification of a raised exception
EAccessViolation when I try to do, say, vInt.resize(1) in the cpp
file as shown below. I would appreciate any help explaining what I
am doing wrong.
Thanks,
Roger
#ifndef MyUtilitiesH
#define MyUtilitiesH
#include <vector>
class TUtilities
{
bool __fastcall TUtilities::Test();
std::vector<int> vInt;
};
extern TUtilities *MyUtils;
#endif
#include "MyUtilities.h"
TUtilities *MyUtils;
Is MyUtils ever modified so as to point to a TUtilities object?
Post by Roger
bool __fastcall TUtilities::Test()
{
vInt.resize(1); // When I run it I get an Access Violation error
If MyUtils remains equal to 0, an access violation is to be expected.


Otherwise, my guess is that the problem is in the code that you don't
show us.

Please reduce your code to the minimal (<50 lines) snippet that allows
your audience to copy & paste & compile & run & see what you are
seeing.
Roger
2008-05-09 16:58:17 UTC
Permalink
My problem is with calling any vector function in that location. If I
declare the vector in the cpp file it works fine, it is only when I put
the declaration in the header file that I am having a problem.
Post by Thomas Maeder [TeamB]
[Please next time choose a more appropriate Subject: header; AFAICT,
your problem is not with declaring a vector, but resizing
one. Thanks!]
Post by Roger
I have a simple test running on BCB 6 in a Console Application that
gives a Debugger Exception Notification of a raised exception
EAccessViolation when I try to do, say, vInt.resize(1) in the cpp
file as shown below. I would appreciate any help explaining what I
am doing wrong.
Thanks,
Roger
#ifndef MyUtilitiesH
#define MyUtilitiesH
#include <vector>
class TUtilities
{
bool __fastcall TUtilities::Test();
std::vector<int> vInt;
};
extern TUtilities *MyUtils;
#endif
#include "MyUtilities.h"
TUtilities *MyUtils;
Is MyUtils ever modified so as to point to a TUtilities object?
Post by Roger
bool __fastcall TUtilities::Test()
{
vInt.resize(1); // When I run it I get an Access Violation error
If MyUtils remains equal to 0, an access violation is to be expected.
Otherwise, my guess is that the problem is in the code that you don't
show us.
Please reduce your code to the minimal (<50 lines) snippet that allows
your audience to copy & paste & compile & run & see what you are
seeing.
Ed Mulroy [TeamB]
2008-05-09 15:50:13 UTC
Permalink
Post by Roger
class TUtilities
{
bool __fastcall TUtilities::Test();
Why "TUtilities::Test();"? In that context, correct syntax is "Test();"

. Ed
Roger
2008-05-09 17:00:07 UTC
Permalink
Post by Ed Mulroy [TeamB]
Why "TUtilities::Test();"? In that context, correct syntax is "Test();"
I don't know, I have done it both ways for years and never had a
problem. I just assumed my way was redundant but not wrong. Am I wrong?
Ed Mulroy [TeamB]
2008-05-09 17:45:09 UTC
Permalink
I see nothing in the standard supporting or suggesting that syntax. However
the compiler seems to accept it.

. Ed
Post by Ed Mulroy [TeamB]
Why "TUtilities::Test();"? In that context, correct syntax is "Test();"
I don't know, I have done it both ways for years and never had a problem.
I just assumed my way was redundant but not wrong. Am I wrong?
Remy Lebeau (TeamB)
2008-05-09 16:24:40 UTC
Permalink
Post by Roger
I have a simple test running on BCB 6 in a Console Application
that gives a Debugger Exception Notification of a raised exception
EAccessViolation when I try to do, say, vInt.resize(1) in the cpp
file as shown below. I would appreciate any help explaining what
I am doing wrong.
How are you actually calling Test()? I suspect that you are accessing it
via an invalid TUtilities pointer.


Gambit
Roger
2008-05-09 17:02:24 UTC
Permalink
Post by Remy Lebeau (TeamB)
How are you actually calling Test()? I suspect that you are accessing it
via an invalid TUtilities pointer.
I am using:

MyUtils->Test();

which seems to work fine, it compiles and executes fine... but without
the vector function.
Remy Lebeau (TeamB)
2008-05-09 17:50:27 UTC
Permalink
Post by Roger
MyUtils->Test();
But, did you actually allocate an object instance for MyUnits to point to
first? I am guessing that you did not. You only declared the pointer
itself, but did not point it to anything.


Gambit
Roger
2008-05-09 18:53:37 UTC
Permalink
You are both so right, how embarrassing, I do know that but couldn't
figure it out!

Thanks Remy and Ed.
Post by Remy Lebeau (TeamB)
Post by Roger
MyUtils->Test();
But, did you actually allocate an object instance for MyUnits to point to
first? I am guessing that you did not. You only declared the pointer
itself, but did not point it to anything.
Gambit
Ed Mulroy [TeamB]
2008-05-09 19:25:04 UTC
Permalink
Don't be embarrassed. I guarantee that all of us have also done that at
some time.

. Ed
Post by Roger
You are both so right, how embarrassing, I do know that but couldn't
figure it out!
Thanks Remy and Ed.
Ed Mulroy [TeamB]
2008-05-09 17:47:29 UTC
Permalink
Your code includes this comment
// When I run it I get an Access Violation error

What you posted cannot run as it has no function main.

When this is added it runs without a problem.

int main(int, char *[])
{
MyUtils = new TUtilities;
MyUtils->Test();
return 0;
}

When this is used instead it correctly generates an exception because the
pointer MyUtils is NULL.

int main(int, char *[])
{
MyUtils->Test();
return 0;
}

. Ed
I have a simple test running on BCB 6 in a Console Application that gives a
Debugger Exception Notification of a raised exception EAccessViolation when
I try to do, say, vInt.resize(1) in the cpp file as shown below. I would
appreciate any help explaining what I am doing wrong.
Thanks,
Roger
#ifndef MyUtilitiesH
#define MyUtilitiesH
#include <vector>
class TUtilities
{
bool __fastcall TUtilities::Test();
std::vector<int> vInt;
};
extern TUtilities *MyUtils;
#endif
#include "MyUtilities.h"
TUtilities *MyUtils;
bool __fastcall TUtilities::Test()
{
vInt.resize(1); // When I run it I get an Access Violation error
return true;
}
Liz
2008-05-11 13:55:33 UTC
Permalink
Post by Roger
bool __fastcall TUtilities::Test()
{
vInt.resize(1); // When I run it I get an Access Violation error
of course you do - you never allocated MyUtils did you?
Post by Roger
return true;
}
--
liz
Roger
2008-05-12 20:06:53 UTC
Permalink
Thank you for confirming what Ed and Remy already told me.
Post by Liz
Post by Roger
bool __fastcall TUtilities::Test()
{
vInt.resize(1); // When I run it I get an Access Violation error
of course you do - you never allocated MyUtils did you?
Post by Roger
return true;
}
Liz
2008-05-12 21:03:25 UTC
Permalink
Post by Roger
Thank you for confirming what Ed and Remy already told me.
I saw that thay'd answered after I sent. But I'm hppy to add my vote
to what they already defined for you :)
--
liz
Loading...