0

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.

Robert.O
  • 1
  • 1
  • 2
    I don’t see you ever putting anything to `students` so dereferencing the pointers in there is undefined behavior. Did you forget to add that part to `parseThenAdd`? – Sami Kuhmonen May 10 '20 at 15:57
  • 1
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5910058) – Jesper Juhl May 10 '20 at 16:01
  • 1
    It's good to try and remove irrelevant code, but it defeats the purpose if you end up deleteing the part that is actually causing the error. Please make sure that after removing code that what you post is still a compilable program that has the issue you are asking about. – john May 10 '20 at 16:04
  • When you see a number like `0xCDCDCDCD` that is too regular to be an accident or forms a word or a phrase (0xDEADBEEF, for example), the program is probably trying to tell you something. Here's [a good list of common "Magic" debug codes](https://en.wikipedia.org/wiki/Magic_number_(programming)#Debug_values). – user4581301 May 10 '20 at 16:34

0 Answers0