I have been writing this code and I hit a wall with this access violation. I believed it may have been an issue with running out of the array in the for loop for(int i = 0; i <= this->lastIndex; i++), but I can't figure out what to change in there(if anything). Simplified version of code:
Test.h
class Test
{
public:
void setID(string ID);
void setName(string name);
virtual void print() = 0;
string getID();
string getName();
Test();
Test(string ID, string name);
~Test();
protected:
string ID;
string name;
Test.cpp
#include "Test.h"
Test::Test()
{
this->ID = "";
this->name = "";
}
Test::Test(string ID, string name)
{
this->ID = ID;
this->name = name;
}
string getID()
{
return ID;
}
string getName()
{
return name;
}
void Test::setID(string ID)
{
this->ID;
}
void Test::setName(string name)
{
this->name;
}
void Test::print()
{
cout << ID;
cout << name;
}
Test::~Test() {}
RosterTest.h
#include "Test.h"
class RosterTest
{
private:
int lastIndex;
int capacity;
Test** tests;
public:
RosterTest();
RosterTest(int capacity);
void parseThenAdd(string datarow);
void print_All();
~RosterTest();
}
RosterTest.cpp
#include "RosterTest.h"
RosterTest::RosterTest()
{
this->capacity = 0;
this->lastIndex = -1;
this->tests= nullptr;
}
RosterTest::RosterTest(int capacity)
{
this->capacity = capacity;
this->lastIndex = -1;
this->tests= new Test*[capacity];
}
void RosterTests::parseThenAdd(string row)
{
if(lastIndex < capacity)
{
lastIndex++;
int rhs = row.find(",");
string ID = row.substr(0, rhs);
int lhs = rhs + 1;
int rhs = row.find(",",lhs);
string name = row.substr(lhs, row.length() - lhs);
}
}
void RosterTest::print_All()
{
for(int i = 0; i <= this->lastIndex; i++)
{
(this->tests)[i]->print(); // ERROR HERE
}
}
RosterTest::~RosterTest()
{
for(int i = 0; i <=lastIndex; i++)
{
delete this->tests[i];
}
delete tests;
}
main.cpp
#include "RosterTest.h"
int main()
{
const int numTests= 2;
string testData[numTests] =
{
"test1,name1",
"test2,name2"
};
RosterTest* ros = new RosterTest(numTests);
for (int i = 0; i < numTests; i++);
{
ros->parseThenAdd(testData[i])
}
ros->print_All(); //ERROR HERE
Where I marked //ERROR HERE is where the errors are located as far as I know, using print_All() fails on the first loop and I'm unsure why.