I'm newish to c++ and even newer to ubuntu

I tried linking and this is what I got:

g++ -o calculator *.o -lcrypt -lm
main.o: In function `main':
main.cpp: (.text+0x1ea): undefined reference to `SAMSErrorHandling::Initialize()'
main.cpp: (.text+0x2fa): undefined reference to `SAMSErrorHandling::HandleNotANumberError()'
main.cpp: (.text+0x322): undefined reference to `SAMSPrompt::UserWantsToContinueYOrN(char const*)'
collect2: ld returned 1 exit status


let it be noted I've never linked anything using command line
Dev-C++ used to magically do all that stuff for me.
Not sure what the problem is...
Someone reccomended I put "using namespace SAMSErrorHandling" in main.cpp but the error message remains the same
everything compiles fine, it's just putting these all together into something I can run that's giving me the probelm

here's main.cpp:

#include <iostream>
#include <stdexcept>
#include "PromptModule.h"
#include "ErrorHandlingModule.h"

using namespace std;


char GetOperator(void)
{
	char theOperator;
	cout << "Operator: ";
	cin >> theOperator;
	return theOperator;
}

float GetOperand(void)
{
	float theOperand = 1;
	cout << "The Operand: ";
	cin >> theOperand;
	return theOperand;
}
	
	



float Accumulate (const char theOperator,const float theOperand)
	{
		static float myAccumulator = 0; //Inititalize to 0 when the program starts
		switch (theOperator)
		{
			case '+':
				myAccumulator = myAccumulator + theOperand;
				break;

			case '-':
				myAccumulator = myAccumulator - theOperand;
				break;
			case '*':
				myAccumulator = myAccumulator * theOperand;
				break;
			case '/':
				myAccumulator = myAccumulator / theOperand;
				break;
			default:
				throw
					runtime_error("Error - Invalid Operator");
		};
	return myAccumulator;
	}





int main(int argc, char* argv[])
{
	SAMSErrorHandling::Initialize();
	do
	{
		try
		{
			char Operator = GetOperator();
			float Operand = GetOperand();
			cout << Accumulate(Operator,Operand) << endl;
		}
		catch (runtime_error RuntimeError)
		{
			SAMSErrorHandling::HandleRuntimeError(RuntimeError);
		}
		catch (...)
		{
			SAMSErrorHandling::HandleNotANumberError();
		};
	}
	while (SAMSPrompt::UserWantsToContinueYOrN("More ?"));
	return 0;
}

here are the two headers in case I messed those up:

#ifndef PromptModuleH
#define PromptModuleH


namespace SAMSPrompt
{
bool UserWantsToContinueYOrN (const char *theThingWeAreDoing);
}
#endif

and the second one:

#ifndef ErrorHandlingModuleH
#define ErrorHandlingModuleH
#include <stdexcept>

using namespace std;

namespace SAMSErrorHandling
{
	void Initialize(void);
	int HandleNotANumberError(void); //Returns error code
	int HandleRuntimeError(runtime_error theRuntimeError);
}

#endif

thanks in advance.

Recommended Answers

All 4 Replies

If you could list your files, I will show how to compile and link them

well these are the rest of the files i haven't showed

ErrorHandlingModule.cpp

#include <iostream>
#include <stdexcept>
#include "ErrorHandlingModule.h"

using namespace std;

namespace SAMSErrorHandling
{
	using namespace std;
	
	void Inititalize(void)
	{
		cin.exceptions(cin.failbit);
	}
	
	int HandleNotAnumberError(void) //Returns error code
	{
		cerr << "Input error - not a number?" << endl;
		cin.clear();
		//Eat the bad input so we can pause the program
		char BadInput[5];
		cin >> BadInput;
		return 1; // An error occured
	}
	int HandleRuntimeError(runtime_error theRuntimeError)
		{
			cerr << theRuntimeError.what() << endl;
			return 1;
		}
}

andd......
PromptModule.cpp

#include <iostream>
#include "PromptModule.h"

using namespace std;

namespace SAMSPrompt
{

void PauseForUserAcknowledgement(void)
{
	//Note: You must type something before Enter
	char StopCharacter;
	cout << endl << "Press a key and \"Enter\": ";
	cin >> StopCharacter;
}

bool UserWantstoContinueYOrN (const char *theThingWeAreDoing)
	{
		char DoneCharacter;
		bool InvalidCharacterWasEntered = false;
		do
		{
			cout << endl << theThingWeAreDoing << " - Press \'n\' and \'Enter\' to stop: ";
			cin >> DoneCharacter;

			InvalidCharacterWasEntered = 
			!
			(
				(DoneCharacter == 'y')
				||
				(DoneCharacter == 'n')
			);

				if (InvalidCharacterWasEntered)
					{
						cout << "...Error - " << "please enter \"y\" or \"n\"."<<endl;
					};
		}while (InvalidCharacterWasEntered);
	return (DoneCharacter != 'n');

	}
}

I've got them all to object files with no problem...
never linked before though x_x

All I was looking for was the file names....Oh well
If you have the files compiled to object files then all you have to do is collect them together for the final linking/compiling...

g++ ErrorHandlingModule.o PromptModule.o main.o -o exefilename -lcrypt -lm

I'm assuming the switches at the end are correct(I got them from your original posting)

Also a great thing to learn is creating makfiles...here's a link

http://makepp.sourceforge.net/1.19/makepp_tutorial.html

tried that but I'm still getting that same error message
somewhere in my code I must have messed something up
as I'm pretty sure I'm using the correct commands to link it
:-/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.