-4

I am having trouble getting some code to compile. The problem seems to be with assigning an array reference to an element of a struct. Here is the code below, and the error message.

So I have a struct that looks like this

struct numpyFormatterData{
    double* pointerToNumpyData;
    unsigned long numberOfDataRows;
    unsigned long numberOfDataColumns;
    std::string delimitedStringOfColumnNames;
};

Then I have a function that generates a random 50x10 array of doubles.

double generateRandomArray(double lower_, double upper_)
{
    int row = 50;
    int columns = 10;
    double arr[rows][columns];

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            arr[i][j] = std::uniform_real_distribution<double>(lower_, upper_)(gen);
        }
    }
    return arr;
}

In my test case I have the following code which is causing the problem. I am using the Catch testing framework.

TEST_CASE( "Test construction of NumpyDataRequest Class", "[NumpyDataRequest]" ) {

SECTION( "Test factory method for creating NumpyDataRequest" ) {

    double testDataArray = generateRandomArray(0.0, 2.0);
    numpyFormatterData testNumpyRequest;
    testNumpyRequest.numberOfDataRows = 50;
    testNumpyRequest.numberOfDataColumns = 10;
    testNumpyRequest.delimitedStringOfColumnNames;
    testNumpyRequest.pointerToNumpyData = &testDataArray; //PROBLEM HERE

    std::unique_ptr<NumpyDataRequest> testingDataRequest = NumpyDataRequest::setDataForRequest(testNumpyRequest);
    numpyFormatterData checkDataRequest = testingDataRequest->getRequestData();
    REQUIRE(checkDataRequest.numberOfDataColumns == 10);


}
}

The error message I am getting is:

undefined reference to `generateRandomArray(double, double)'
collect2: error: ld returned 1 exit status

I know that I am trying to assign the reference to testDataArray incorrectly. I have tried a variety of different ways to do this, but none have worked. Any suggestions?

krishnab
  • 9,270
  • 12
  • 66
  • 123
  • I see a number of problems. First of all, according to your error text, there is just no generateRandomArray function included in the scope of test file. Secondly, there is no result field in your function you can assign somethin to. And finally, it seems that result is suggested to be not double * but double **. Please check it. – Arsenii Fomin Apr 20 '17 at 05:22
  • result is not declared within `generateRandomArray(...)` but let's assume that it is declared somewhere else and is accessible you still cannot `return result;` because the function returns a double also you cannot return an array in c++, you return a pointer to the first element instead. – Falla Coulibaly Apr 20 '17 at 05:26
  • @ArseniiFomin so actually the problem is not the generateRandomArray function. I have an `include` in the test file for the generateRandomArray function, so that does not seem to be the issue. Now when I remove the `&` amperstand in front of `testDataArray`, then I get an error `cannot convert ‘double’ to ‘double*’ in assignment testNumpyRequest.pointerToNumpyData = testDataArray;` which is why I am think the error is located with that assignment. – krishnab Apr 20 '17 at 05:27
  • See comment above from Falla Coulibaly. You really need pay attention to result declaration, because if it is declared beyond your function it is very bad style. Anyway, whey you will find it (declaration), according to it's type you will understand what type do you need in assignment. – Arsenii Fomin Apr 20 '17 at 05:32
  • The error message simply means that you are not including all relevant source or object files in the build. See http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix There are many problems in the code itself but the error message is not related to them. – n. m. could be an AI Apr 20 '17 at 05:49
  • @n.m. Thanks, yep I caught that. I had added the `include`, but forgot to add it to `cmake.` I fixed that though. Thanks for the tip. – krishnab Apr 20 '17 at 05:50

1 Answers1

0

If what you are looking for is a reference to your array then change the return type of double generateRandomArray(...) to double* generateRandomArray(...) then return the reference like this return &result[0][0]; and finally change double testDataArray to double *testDataArray.

Edit: I just noticed that your array is allocated on the stack which is not what you want in this example. Consider using the heap instead or a vector.

Falla Coulibaly
  • 759
  • 2
  • 11
  • 23
  • Okay thanks. So I am with you on the generateRandomArray. I actually return `arr`--seems I put up an older version of that function--so I will fix that. I am with you on the function returning a pointer to the array. Thanks for clarifying on how to fix this. I will give it a shot. – krishnab Apr 20 '17 at 05:34
  • hey Falla. Haha, I would love to use a vector, but cannot in this case. So I am actually trying to connect from python numpy to C++. Numpy exposes a basic pointer to its underlying C array. So I have to recover that point on the C++ side. Once I have it in C++, then I actually will generate a vector--but first I just have to catch the pointer that numpy sends. But your comments here are really really helpful. I am kinda new to C++ programming, so just finding my way through. – krishnab Apr 20 '17 at 05:42