简体   繁体   中英

Undefined reference when passing 2D array to function

I'm writing a code that takes 2D arrays and puts them into functions. They are then doing stuff with it and updating it. For a reason unknown to me however, I'm getting an undefined reference error even though I spent much time reading threads where people explained how to properly link .cpp and .h files. Let me show you the simplified code:

header.h

#ifndef HEADER_H
#define HEADER_H

const int N = 2;
const int rows = 206;
const double ptoSI = 2;


void getEOS(double etable[206][N], double ptable[206][N], double c_stable[206][N]);
#endif // HEADER_H

header.cpp

#include <iostream>
#include <fstream>
#include <math.h>
#include "header.h"
 using namespace std;

void getEOS(double etable[206][N], double ptable[206][N], double c_stable[206][N]){

       for (int i = 0; i<rows; i++){
        for (int j = 0; j<N; j++){

         etable[i][j] = cache[i][j*3]*1.60218E-13*1E45*1.0/(ptoSI);
          ptable[i][j] = cache[i][j*3+1]*1.60218E-13*1E45*1.0/(ptoSI);
          c_stable[i][j] = cache[i][j*3+2];

        }
    }

    }

main.cpp

#include <iostream>
#include "header.h"

using namespace std;

int main()
{

double etable[rows][N];
double ptable[rows][N];
double c_stable[rows][N];

getEOS(etable,ptable,c_stable);


}

The error reads in detail:

/usr/bin/ld: main.o: in function `main':
/home/tux/build-testtext-Desktop_Qt_5_15_2_GCC_64bit-Debug/../testtext/main.cpp:13: undefined reference to `getEOS(double (*) [2], double (*) [2], double (*) [2])'
collect2: error: ld returned 1 exit status
make: *** [Makefile:274: testtext] Error 1

Why does he have a problem with this, even though I declared and defined the function properly?

Any help would be appreciated...

.pro file:

TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt

SOURCES += \
        header.cpp \
        main.cpp

HEADERS += \
    header.h

I am using the following kit: Desktop Qt %{Qt:Version} GCC 64bit

Sorry,

if this code is professional, I would not let it pass in a review:

  1. You are not using std::array
  2. You are using a random constant 206, and not declare it this way
  3. You are iterating over a matrix which is probably 206xN big. However, you are iterating over some number "rows" which is probably equal to 206. So you should probably use "rows" accordingly.
  4. The code block where you access some member from "cache" is atrocious. I would fire people who would make me write code like this.
  5. 1.60218E-13*1E45 is probably some very important physical constant. I would define it ONCE in some suitable header. It could probably be described as 1.60218e-32.
  6. In your main function: Where does that ominous "row" parameter come from?
  7. Usually, when you define a "Getter" function, you get something. Your function does not provide anything, it is void.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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