Linker error

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 header
#ifndef CHRANGE_H
#define CHRANGE_H

class CharRange
{
private:
char input; //User input
char lower; //lowest valid character
char upper; //Highest valid character

public:
CharRange(char, char);
char getChar();
};

#endif




//Functions definitions
//CharRange.cpp

#include<iostream>
#include<cctype>
#include "CharRange.h"
using namespace std;

//*********************************************
// CharRange constructor *
//*********************************************
CharRange::CharRange(char ,char u)
{
lower = toupper(l);
upper = toupper(u);
}

//*********************************************
// 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;
}
Make sure you are linking against CharRange.o(bj) also.
But how can I do that?
Thanks
With gcc:

> g++ -c CharRange.cpp

Creates CharRange.o

> g++ -c main.cpp

Creates main.o

> g++ CharRange.o main.o -o main

Links CharRange.o and main.o together and outputs it as main (or main.exe in Cygwin)
Sorry but I still dont get it ....
Is there something I have to write in any of the codes?

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.
Topic archived. No new replies allowed.