Hi I wrote a function that takes csv file, read its (numeric) content and convert it into 2darray and return the pointer to this array. But when I pass in int const & row, int const & col, it gives the following linker error:
Undefined symbols for architecture x86_64:
"csv2array(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, unsigned long, unsigned long)", referenced from:
_main in csv_to_array-80e056.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Here is my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
double** csv2array(string filename, const size_t row, const size_t col);
/* This function converts csv file into 2-d array
* used to convert numpy 2d array into c++ array
* filename -- csv file containing 2d matrix
* row, col -- dimension of the matrix
*/
int main() {
const size_t SIZE = 103; // sample size
double **odM = csv2array("odMat.csv", SIZE, SIZE);
double **Dist = csv2array("dist.csv", SIZE, SIZE);
for (size_t i=0; i<SIZE; i++) {
for (size_t j=0; j<SIZE; j++) {
cout << odM[i][j] << "\t";
}
cout << endl;
}
for (size_t i=0; i<SIZE; i++) {
delete [] odM[i];
delete [] Dist[i];
}
delete [] odM;
delete [] Dist;
return 0;
}
double** csv2array(string filename, const size_t &row, const size_t &col) {
ifstream file(filename); // associate a buffer with filename
double **mat = new double*[row]; // note need to release in main function!
for (size_t i=0; i<row; i++) {
mat[i] = new double[col];
string element;
getline(file, element); // read a whole line into csv file, have to return string type
if ( !file.good() )
break;
stringstream iss(element);
for (size_t j=0; j<col; j++) {
string val;
getline(iss, val, ',');
if ( !iss.good() )
break;
stringstream convertor(val);
convertor >> mat[i][j];
}
}
return mat;
}
|
If i remove alias symbol, it compiles fine. It achieves my goal here. But I am just curious why const int & row will give such an error. Thanks in advance!