getting output from a function in c++

167 views Asked by At

I have written the following code using armadillo in c++. However, I cannot get any output (my outputs are cordX and cordY). Is there something wrong with my definition that it cannot give any output?

#include <iostream>
#include<armadillo>

using namespace std;
using namespace arma;

void myOrder(const mat &sOut, const mat &T, const mat &OL, mat &cordX, mat &cordY);

int main()
{
    mat sOut;
    sOut << 100 << 120 << endr;

    mat T;
    T << 20 << 20 << endr;

    mat OL;
    OL << 5 << 5 << endr;

    mat locX;
    mat locY;

    myOrder(sOut, T, OL, locX, locY);

    cout << locX << endl;
    cout << locY << endl;

    system("PAUSE");
    return 0;
}

void myOrder(const mat &sOut, const mat &T, const mat &OL, mat &cordX, mat &cordY)
{
    vector<double> U;
    for (int i = 0; i < sOut(0) - T(0); i += T(0) - OL(0))
    {
        U.push_back(double(i));
    }


    vector<double> V;
    for (int j = 0; j < sOut(1) - T(1); j += T(1) - OL(1))
    {
        V.push_back(double(j));
    }


    if ((double(U.back()) + 2*T(0) - OL(0) - 1) > sOut(0) && (double(U.back()) + T(0) !=sOut(0)))
    {
        U.push_back(sOut(0) - T(0) + 1);
    }
    colvec cordX = conv_to< colvec >::from(U);



    if ((double(V.back()) + 2*T(1) - OL(1) - 1) > sOut(1) && (double(V.back()) + T(1) !=sOut(1)))
    {
        V.push_back(sOut(1) - T(1) + 1);
    }
    colvec cordY = conv_to< colvec >::from(V);

    // return cordX, cordY;
}
2

There are 2 answers

5
Some programmer dude On

You declare local variables with the same name as two of the arguments to the function, cordX and cordY. Declaring them locally shadows the arguments, so the arguments will no longer be available to you. Building with more warning enabled should have led the compiler to give you warnings about this.

I assume you meant to assign to the arguments, and not create two new variables.

0
savitha A On

Myorder function stack will store the result to local variable cordX instead of reference to locX.

  1. colvec cordX = conv_to< colvec >::from(U);

so this locX is NULL.

You can do this and respectively to CordY

cordX = conv_to< colvec >::from(U);