Despot Despondency

November 17th, 2008, 08:07 PM

Hi, I'm having some problems using header files with C++. I've got my main program, which calculates a specified number of squares and prints them off in aligned columns. It's kept in a file called exercise4_2.cpp as follows

#include <iostream>

#include <algorithm>

#include <string>

#include <vector>

#include <ios>

#include <iomanip>

#include "numberOfDigits.h"

using std::vector;

using std::cout;

using std::endl;

using std::setw;

using std::cin;

int main(){

cout << "Enter the number of squares you wish to calculate: ";

int n = 0;

cin >> n;

vector<int> squares;

for(int i = 0; i < n; i++){

squares.push_back(i*i);

}

int square_length = numberOfDigits(squares[n - 1]);

int length = numberOfDigits(n - 1);

for(int i = 0; i < n; i++){

cout << setw(length);

cout << i;

cout << setw(square_length + 1);

cout << squares[i] << endl;

}

return 0;

}

It uses a function called numberOfDigits, which I wrote, and which is kept in the file numberOfDigits.cpp which is given by

#include "numberOfDigits.h"

int numberOfDigits(int n)

{

int length = 0;

while(n >= 1){

length++;

n = n/10;

}

return length;

}

and then I've got my header file, numberOfDigits.h, which is

#ifndef GUARD_numberOfDigits_h

#define GUARD_numberOfDigits_h

int numberOfDigits(int);

#endif

Now when I try to compile exercise4_2.cpp I get the following error

g++ -o exercise4_2 ./exercise4_2.cpp

/tmp/cccmXCrU.o: In function `main':

exercise4_2.cpp:(.text+0x230): undefined reference to `numberOfDigits(int)'

exercise4_2.cpp:(.text+0x241): undefined reference to `numberOfDigits(int)'

collect2: ld returned 1 exit status

I'm confused. How come it can't find the function numberOfDigits?

#include <iostream>

#include <algorithm>

#include <string>

#include <vector>

#include <ios>

#include <iomanip>

#include "numberOfDigits.h"

using std::vector;

using std::cout;

using std::endl;

using std::setw;

using std::cin;

int main(){

cout << "Enter the number of squares you wish to calculate: ";

int n = 0;

cin >> n;

vector<int> squares;

for(int i = 0; i < n; i++){

squares.push_back(i*i);

}

int square_length = numberOfDigits(squares[n - 1]);

int length = numberOfDigits(n - 1);

for(int i = 0; i < n; i++){

cout << setw(length);

cout << i;

cout << setw(square_length + 1);

cout << squares[i] << endl;

}

return 0;

}

It uses a function called numberOfDigits, which I wrote, and which is kept in the file numberOfDigits.cpp which is given by

#include "numberOfDigits.h"

int numberOfDigits(int n)

{

int length = 0;

while(n >= 1){

length++;

n = n/10;

}

return length;

}

and then I've got my header file, numberOfDigits.h, which is

#ifndef GUARD_numberOfDigits_h

#define GUARD_numberOfDigits_h

int numberOfDigits(int);

#endif

Now when I try to compile exercise4_2.cpp I get the following error

g++ -o exercise4_2 ./exercise4_2.cpp

/tmp/cccmXCrU.o: In function `main':

exercise4_2.cpp:(.text+0x230): undefined reference to `numberOfDigits(int)'

exercise4_2.cpp:(.text+0x241): undefined reference to `numberOfDigits(int)'

collect2: ld returned 1 exit status

I'm confused. How come it can't find the function numberOfDigits?