任何人都可以尽可能简单地问一下这段代码的原因:

LinkedListByGrade::LinkedListByGrade(vector<Node> myNodes) {
int lowestGradeFound = myNodes[0].getGrade();
Node *pFristNode = new Node();
cout << "New node created" << endl;
for (size_t i = 0; i <= myNodes.size(); i++) {
    if (myNodes[i].getGrade() < lowestGradeFound) {

        cout << "if triggered" << endl;
        lowestGradeFound = myNodes[i].getGrade();
        pFristNode->reassasignNode(myNodes[i].getFristName(),
                myNodes[i].getLastName(), myNodes[i].getId(),
                myNodes[i].getGrade());
        cout << "int reassassigned" << endl;
    }


给我这个错误:

1 [main] Project 972 exception::handle: Exception: STATUS_ACCESS_VIOLATION
2130 [main] Project 972 open_stackdumpfile: Dumping stack trace to Project.exe.stackdump


以及如何解决的想法?

(对不起,我在诊断打印声明中留下了。)

这也是影响myNodes的唯一代码:

vector<Node> Students;
Node node1("Bobby", "zilch",28,55);
Node node2("Evil", "Dentist",308,55);
Node node3("Raz", "Buton",10,55);
Students.push_back(node1);
Students.push_back(node2);
Students.push_back(node3);

cout<<"stuff"<<endl;
 LinkedListByGrade myList = LinkedListByGrade(Students);

最佳答案

我不能肯定地说,但是您正在访问未分配给您的内存,因此很可能您正在myNodes的边界上运行。您将可以使用调试器来确定这一点。

我可以肯定地说的是,您在函数的第一行中调用了:

int lowestGradeFound = myNodes[0].getGrade();


如果myNodes中没有任何项目,则这将导致访问冲突。

当然,您正在运行myNodes循环中for的结尾,因为您正在访问myNodes[myNodes.size()]

尝试这样的事情:

LinkedListByGrade::LinkedListByGrade(vector<Node> const& myNodes) {
    // Pass by const-reference! ------------------^

    // Set this to something high. Don't access the vector, in case there are no
    // elements therein:
    int lowestGradeFound = std::numeric_limits<int>::max();

    // Where is this deleted?
    Node *pFirstNode = new Node();

    // Use iterators, not indices for looping over a container...
    for (vector<Node>::iterator i = myNodes.begin(), end = myNodes.end(); != end; ++i)
    {
        // Access element by `i->`
        if (i->getGrade() < lowestGradeFound)
        {
            // Now you can assign this safely...
            lowestGradeFound = i->getGrade();
            pFirstNode->reassasignNode(
                i->getFirstName(),
                i->getLastName(),
                i->getId(),
                i->getGrade());
        }
    }

    // [...]
}


但是您确实非常需要学习使用调试器!

10-06 09:11