简体   繁体   English

从传递给C ++函数的指针向量访问矩阵

[英]Accessing Matrices from Vector of Pointers Passed to Function in C++

I started writing in C++ a few months ago and am having some trouble using pointers. 几个月前,我开始用C ++编写程序,在使用指针时遇到了一些麻烦。 In the code that I've been writing for work, I've defined several matrices using the matrix_t class that I wrote. 在我为工作编写的代码中,我已经使用编写的matrix_t类定义了几个矩阵。 I created a vector that contains pointers to these matrices. 我创建了一个包含指向这些矩阵的指针的向量。 I pass the vector to a function which accesses the matrices. 我将向量传递给访问矩阵的函数。 Here's a simple example of what I'm trying to do: 这是我要执行的操作的一个简单示例:

#include <iostream>

#include "matrix_t.h"

using namespace std;

int mat_access (vector <matrix_t<int>*> &pmatrices)
{
    matrix_t<int> mat = *pmatrices[0];
    return mat.get_cell(0, 0);
    /*get_cell(r, c) returns the value at row r, column c*/
}

int main()
{
    vector <matrix_t<int>*> pmatrices(1);

    matrix_t <int> mat (1, 1, 1);
    /*mat_1(v, r, c) has r rows, c columns, and is filled with the value v*/
    pmatrices[0] = &mat;

    for (size_t i = 0; i < 5; i ++)
    {
        int k = mat_access(pmatrices);
        cout << k;
    }
}

If I step through the function while debugging, it works the first time the function mat_access is called. 如果我在调试时逐步执行该功能,则该功能将在第一次调用mat_access函数时起作用。 However, the second time that get_cell is called (even though its for the same row and column of the same matrix) a breakpoint is triggered and I get an error message in the output that says an invalid address is being accessed. 但是,第二次调用get_cell(即使它是针对同一矩阵的相同行和列的)也触发了断点,并且我在输出中收到一条错误消息,指出正在访问无效地址。 I'm using App Verifier and the call stack location it returns has no source code. 我正在使用App Verifier,它返回的调用堆栈位置没有源代码。 If I don't pass the vector of pointers to a function, but instead just access it directly in the main method, it works fine. 如果我不将指针向量传递给函数,而是直接在main方法中对其进行访问,则它可以正常工作。 Also, If I run the same code using vectors instead of matrices, it works fine. 另外,如果我使用向量而不是矩阵运行相同的代码,则效果很好。 I don't think it's a problem with get_cell because I've used it at several other places in my code and I'm sure the indexing is correct. 我认为get_cell没问题,因为我在代码中的其他几个地方都使用过它,并且我确定索引是正确的。 Here's what it looks like within the matrix_t class: 这是matrix_t类中的外观:

T get_cell (size_t row, size_t col) const
{   try
    {   
        if (row >= n_rows || col >= n_cols)
            throw myerror("Index out of bounds");
    }
    catch (exception &e)
    {
        cout << "Exception: " << e.what() << '\n';
    }
    return t_array [col * n_rows + row];
}

t_array contains the matrix elements and n_rows is the number of rows of the matrix. t_array包含矩阵元素,n_rows是矩阵的行数。 If all else fails, I'll just combine my matrices into one matrix and pass that to the function, but I'd like to understand why this isn't working. 如果所有其他方法都失败了,我将把矩阵合并到一个矩阵中,然后将其传递给函数,但是我想了解为什么这行不通。 Any input would be appreciated. 任何输入将不胜感激。

Syntactically, I don't see any issues with your code. 从句法上讲,我看不到您的代码有任何问题。 Try running this snippet (equivalent to what you have above) and see if you encounter the same error. 尝试运行此代码段(等同于上面的代码),看看是否遇到相同的错误。 If not, most likely the issue is in your matrix code. 如果不是,则很可能是您的矩阵代码中存在此问题。

int foo(vector<int*> &v) {
    int x = *v[0];
    return x;
}

int main() {
    vector<int*> v(1);
    int x = 9;
    v[0] = &x;
    for(int i = 0; i < 5; ++i) {
        int y = foo(v);
        cout << y << endl;
    }

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM