hello everybody,
I'm trying to compile my code and I get this error:
[Linker error] undefined reference to `CharRange::CharRange(char, char)'
[Linker error] undefined reference to `CharRange::getChar()'
[Linker error] undefined reference to `CharRange::getChar()'
ld returned 1 exit status
All files are in the same folder, I'm using Dev c++
Here is the code(3 files):
//*********************************************
// CharRange member function getChar *
// Inputs a character and validates that it *
// is in the correct range. Then it returns *
// the valid character. *
//*********************************************
char CharRange::getChar()
{
cin.get(input); //get a character
cin.ignore(); //ingnore the \n in the input character
input = toupper(input); //Uppercase the character
//ensure character is in the correct range
while(input < lower || input > upper)
{
cout << "That is not a valid character.\n";
cout << "Enter a value from " << lower;
cout << " to " << upper << ".\n";
cin >> get(input);
cin.ignore();
input = toupper(input);
}
return input;
}
//this program uses CharRange class and demonstrates its capabilities
//this file shoud be combined into a project along with CharRange.h
//and CharRange.cpp files
#include<iostream>
#include "CharRange.h"
using namespace std;
int main()
{
char ch; //Holds user input
CharRange input('J','N'); //create a CharRange object called input
// it will check for chars in the range J - N
cout << "Enter any of the characters J,K,L,M or N.\n";
cout << "Entering N will stop this program.\n";
ch = input.getChar();
while( ch != 'N')
{ cout << "You entered " << ch << endl;
ch = input.getChar();
}
return 0;
}
I fixed the problem by pasting function defintions inside header file...
I still cant understand why it didnt link before...
If somebody could explain
Thanks for the answers
Because when you were trying to the link the executable, it was not linking in CharRange.o which contained all of the compiled code for those functions.
You have to ask someone familiar with Dev C++ how to make a multi-source file project. seymore above gave you the shell commands you would need to invoke to compile and link the application using gcc.