Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

memory problem with vector using push_back()

Status
Not open for further replies.

sjohan

Programmer
Mar 20, 2007
2
0
0
DE
Hi!

I have run into a problem that I can't solve on my own. I'm pretty new in C++ programming so I really don't know what has to be defined and what does the compiler define if I leave out. My problem could probably arrise from I'm not having copy constructors defined.

But first I should probably say that I'm using Visual C++ 6.0

In my implementation, I have a "vector<TestCase> testCaseList" where TestCase is defined as following:

#include <string>
#include <map>
#include <vector>


#ifdef _WIN32
using namespace std;
#endif

class TestCase
{
public:
TestCase();
virtual ~TestCase();

map<string, string, std::less<string> > testCaseInfo;

map<int, map<string, string, std::less<string> >, std::less<int> > actionList;

map<int,int, std::less<int> > actionOrder;

map<string, string, std::less<string> > actionParameterList;

vector<string> expectedValueList;

};


When I try to insert a new TestCase into my vector I get an "Unhandled exception ... access violation" error. This is the code which I use to insert the new object in the vector:

testCaseList.push_back(TestCase());


I've tried to debug it in VC++ and as far as I can see the problem arise after the TestCase-constructor has executed and the program is in the push_back() function.

I've read that some one else had a similar problem but his vector contained a class which only had strings as members so someone said that in that case no special copy-constructor was needed. But in my case, when TestCase has maps and vectors as members, do I need to define a copy constructor for my TestCase-class, and in that case how do I do that and can anyone please instruct me what it should do?

thankful every tip I can get for finding a solution
 
1. Use [ignore]
Code:
[/ignore] tags when posting code.

2. Why do you only want your code to be able to compile in Windows?
Code:
#ifdef _WIN32 
using namespace std; 
#endif
You need this line for every compiler/OS.

Instead of this:
Code:
testCaseList.push_back(TestCase());
Try this:
Code:
TestCase testCase;
testCaseList.push_back( testCase );
 
Can't reproduce any troubles with empty ctor/dtor (VC++ 6.0 SP 6).
Present your class ctor/dtor implementations...
 
Just a general comment: you might find it easier to read if you use typedefs
Code:
typedef map<string, string, std::less<string> > StrMap;

class TestCase  
{
public:
    TestCase();
    virtual ~TestCase();
    
    StrMap testCaseInfo;

    map<int, StrMap, std::less<int> > actionList;

    map<int,int, std::less<int> > actionOrder;

    StrMap actionParameterList;

    vector<string> expectedValueList;

};
 
Thanks for all help and tip.

I tried the suggestion with
Code:
TestCase testCase;
testCaseList.push_back( testCase );
but it didn't work. So i found how to implement a copy constructor and that helped for this problem.

Thanks again for your suggestions and tip.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top