1
I am trying to create a library for operations with matrices in C++ but am running into a problem.
I created a function to print on the screen a certain matrix. The function even returns the expected values, however in the last line appears the following error message:
free(): invalid pointer
Abortado Abortado (imagem do núcleo gravada)
Follow the code below:
#include<iostream>
#include<vector>
using namespace std;
vector<vector<double>> imprime(vector<vector<double>> X) {
int linhas = X.size();
int colunas = X[0].size();
for (int i=0; i<linhas; i++) {
for (int j=0; j<colunas; j++) {
cout << X[i][j] << "\t";
}
cout << endl;
}
}
int main() {
vector<vector<double>> X {
{1.0, 2.0, 3.0,2.5},
{2.0, 5.0,-1.0,2.0},
{-1.5,2.7,3.3,-0.8}
};
imprime(X);
return 0;
}
When I try to make the loop go straight into the main function, it works:
#include<iostream>
#include<vector>
using namespace std;
int main() {
vector<vector<double>> X {
{1.0, 2.0, 3.0,2.5},
{2.0, 5.0,-1.0,2.0},
{-1.5,2.7,3.3,-0.8}
};
for (int i=0; i<X.size(); i++) {
for (int j=0; j<X[0].size(); j++) {
cout << X[i][j] << "\t";
}
cout << endl;
}
return 0;
}
In function
vector<vector<double>> imprime(vector<vector<double>> X) {
did not miss a Return? In fact it could not be void?– anonimo