Sep 28, 2016 at 7:42am UTC
what is wrong with this program? It is in 3 separate files and I´m using Xcode.
file print.h
#ifndef print_h
#define print_h
extern const int NCOLS ;
extern const int NROWS ;
void print( double [][NCOLS], int );
#endif /* print_h */
file print.cpp
#include <iostream>
#include <fstream>
#include <iomanip>
#include "print.h"
using namespace std;
void print( double array[][NCOLS], int nrows )
{
ofstream outFile;
outFile.open("tryarray");
for (int i = 0; i < nrows; i++)
{
for (int j = 0; j < NCOLS; j++)
{
cout << array[i][j] << " ";
outFile << fixed << showpoint << setprecision(3);
outFile << setw(8) << array[i][j];
}
outFile << endl;
}
outFile.close();
}
file main.cpp
#include <iostream>
#include <fstream>
#include <iomanip>
#include "print.h"
using namespace std;
const int NROWS = 4;
const int NCOLS = 4;
//void print( double [][NCOLS],const int );
int main(int argc, const char * argv[]) {
double arr[NROWS][NCOLS] = {{1.0, 2.0, 3.0, 4.0}, {5.0, 6.0, 7.0, 8.0},
{9.0, 10.1234, 11.0, 12.0}, {13.0, 14.0, 15.0, 16.0}};
print(arr, NROWS);
return 0;
}
The errors I get:
Undefined symbols for architecture x86_64:
"_NCOLS", referenced from:
print(double (*) [], int) in print.o
"print(double (*) [4], int)", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation
/Users/marioneto/Documents/Diffusion/Diffusion/main.cpp:27:6: No matching function for call to 'print'
Sep 28, 2016 at 8:37am UTC
I think it's because the compiler needs to know the value of NCOLS when you declare and define the print function.
Sep 28, 2016 at 8:37am UTC
-- deleted by myself. wrote silly things ... Sorry. Thanks to peter
Last edited on Sep 28, 2016 at 8:46am UTC