program does not compile

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'

I think it's because the compiler needs to know the value of NCOLS when you declare and define the print function.
-- deleted by myself. wrote silly things ... Sorry. Thanks to peter
Last edited on
CptJY wrote:
I think you should also declare NCOLS and NROWS as external in main.cpp

There is no need to do that because it's done in the print.h header file that has already been included.
Topic archived. No new replies allowed.