not declared in scope and redefining error

Hi I am working along with a beginner c++ book and I have gotten stuck on one part. The program is a calculator and it builds it throughout the book. Unfortunately where I'm stuck it doesn't have the code written out so I can't continue on.

This is where the problem is I will include the main.cpp too.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef ErrorHandlingModuleH
#define ErrorHandlingModuleH
#include <exception>
#include <iostream>



namespace SAMSErrorHandling
{
	
	void Initialize(void);
	int HandleNotANumberError(void);
	int HandleRuntimeError(runtime_error);
}



#endif



...the .cpp file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//#include "StdAfx.h"
#include "ErrorHandlingModule.h"
#include <iostream>
#include <exception>

namespace SAMSErrorHandling{
	
	using namespace std;
	
	void Initialize(void){
		cin.exceptions(cin.failbit);
		}
		
	int HandleNotANumberError(void){
		cerr<< "Input error - not a number?" << endl;
		cin.clear();
		char BadInput[5];
		cin >> BadInput;
		return 1;
		}
	
	int HandleRuntimeError(runtime_error theRuntimeError)
	{
	
	cerr << theRuntimeError.what() << endl;
	return 1;
	}
	
	}
	
	


I get an error saying that runtime_error not defined in scope in the .h file as well as the .cpp

and I get an error on the same lines saying SAMSErrorHandling::HandleRuntimeError is previously defined here....


In the text he says that "To use runtime_error, you need to change ErrorHandlingModule to #include <exception> (which I have done i believe) and add using namespace std wherever the exception is mentioned which I don't really understand where i write this or if this is the cause of the errors.


here is main.cpp if needed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84

//#include "StdAfx.h"
#include <iostream>

#include "PromptModule.h"
#include "ErrorHandlingModule.h"

using namespace std;

char GetOperator(void)
{
	char Opor = '+';
	
	cout << "Operator: ";
	cin >> Opor;
	
	return Opor;
}

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

float Accumulate(const char opor, const float opand)
{
	static float myAccumulator = 0;
	
	switch(GetOperator())
	{
		case '+': myAccumulator = myAccumulator + opand;
		break;
		
		case '-': myAccumulator = myAccumulator - opand;
		break;
		
		case '*': myAccumulator = myAccumulator * opand;
		break;
		
		case '/': myAccumulator = myAccumulator / opand;
		break;
		
		default: throw runtime_error("Error - Invalid operator");
		};
	return myAccumulator;
}

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



I cannot figure this out and it's really annoying I've tried everything I can think of and I don't understand what's wrong.
I just noticed something that's wrong in my code but doesn't seem to effect the errors I'm experiencing ....In line 72 of main.cpp I forgot to pass RunTimeError into HandleRuntimeError();

If you need to see PromptModule for any reason let me know and I will post it.
I have found out that the class runtime_error is in <stdexcept>

when I try to compile while including <stdexcept> I get an undefined reference to winmain@16 and im am compiling a console app from the command line manually. why would this happen??? Do I definetly have acces to stdexcept library would this occur if I didnt?

I had this error before a few chapters earlier and I can't remember how I got it to work
You are probably compiled the application as a Windows app, hence why it is looking for WinMain. Try looking for a switch or option somewhere to compile as a console app.
yeah this was the problem...using gcc I when I don't specify the output file it seems to default to think its a windows app for some reason. thanks
Topic archived. No new replies allowed.