Exit Code 1 and forward declarations

My program has "duplicate symbols" and exits with a code of 1

Page 1 of 1

6 Replies - 726 Views - Last Post: 02 January 2011 - 05:31 PM Rate Topic: -----

#1 treesap526  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 06-December 10

Exit Code 1 and forward declarations

Posted 01 January 2011 - 07:49 PM

Hey guys! Just a quick question. My first is this: When I compile, it says "exit with code 1" and I know that means there are duplicate symbols and I know where the problem is, but I don't know how to fix it. For example:
#include <iostream>
#include <fstream>
using namespace std;

int MathCommand(); //forward declaration needed or else......

int returnToMathMenu() {
cout << "Would you like to return to the math menu?" << endl;
string Answer1;
cin >> Answer1;
if (Answer1 == "Yes") {
MathCommand(); } //.......this does not get defined because its definition comes later on below.
else { cout << endl; }
cin.ignore(1000, '\n');
return 0;}



The definition comes below because it requires functions that are defined above it and each of THOSE functions uses returnToMathMenu(). How do I get the forward declaration to work without triggering the "duplicate symbols" complaint? I'm on a macbook pro 8 with xcode

Is This A Good Question/Topic? 0
  • +

Replies To: Exit Code 1 and forward declarations

#2 jjl  Icon User is online

  • Engineer
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,449
  • Joined: 09-June 09

Re: Exit Code 1 and forward declarations

Posted 01 January 2011 - 08:08 PM

I think you misinterpreted the idea of forward declaration. Your MathCommand() is still defined when you call it in main even if its definition is not within in main()'s scope. Forward declaration gives you the ability to declare a function in such a way that its definition can be used globally (in your case)

This post has been edited by ImaSexy: 01 January 2011 - 08:09 PM

Was This Post Helpful? 0
  • +
  • -

#3 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: Exit Code 1 and forward declarations

Posted 02 January 2011 - 12:39 AM

Look at line 5
You declare a prototype of a function that returns an int.
Look at line 12.
Where are you storing the int the function returns?

Show us the implementation of the function you declared the prototype of.
Does the signature of the implementation match the prototypes signature?
Was This Post Helpful? 0
  • +
  • -

#4 treesap526  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 06-December 10

Re: Exit Code 1 and forward declarations

Posted 02 January 2011 - 01:33 AM

 
int MathCommand() {
	cout << "Math Command?" << endl;
	string commandMath;
	getline(cin,commandMath);
	/*MATH MENU: ADD*/
	if (commandMath == "add" || commandMath == "Add") {
		Add();
		returnToMathMenu();
	}
	/*MATH MENU: MULTIPLY*/
	if (commandMath == "multiply" || commandMath == "Multiply") {
		Multiply();
		returnToMathMenu();
	}
	/*MATH MENU: SUBTRACT*/
	if (commandMath == "subtract" || commandMath == "Subtract") {
		Subtract();
		returnToMathMenu();
	}
	/*MATH MENU: DIVIDE*/
	if (commandMath == "divide" || commandMath == "Divide") {
		Divide();
		returnToMathMenu();
	}
	return 0;
}


That's the implementation of MathCommand(). The function just returns a 0 if all went well basically.
Imasexy: sorry I meant prototype, as Janotte pointed out. I apologize for the miswordage. All of this is in the header of main()
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,217
  • Joined: 23-August 08

Re: Exit Code 1 and forward declarations

Posted 02 January 2011 - 06:06 AM

Do you have multiple files? Please post all of them, in separate sets of code tags.
Was This Post Helpful? 0
  • +
  • -

#6 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: Exit Code 1 and forward declarations

Posted 02 January 2011 - 06:40 AM

Interesting program structure. These calls don't make a lot of sense of presented.
returnToMathMenu();
Why are you calling the calling function?
If you want to get back to the calling function just return.
If you do want to call a function that returns int then make use of the returned int.



Based on what I think you are telling us about MathCommand() it should always return a zero. If it does not return a zero you have an exception event (the program has malfunctioned in an unexpected manner). Is that right?
If that is the case then you should make use of the return value something like this.
if (Answer1 == "Yes") {
    int errorFound = MathCommand();
    assert(!errorFound);
} else { cout << endl; }


This means you are checking that you did, in fact, get back zero and if you didn't you exit the program throwing a message that something horrible and unrecoverable has happened.
You will need
#include <cassert>
to allow the call to assert to compile.

I am not suggesting this is a brilliant option but your choice of returning a zero and ignoring the returned value makes no real sense. If you aren't going to make use if the returned int then just make them void functions. If you are going to return a success value then make use of it.

Also look at the wise advice from JackOfAllTrades if you haven't already.
Was This Post Helpful? 0
  • +
  • -

#7 treesap526  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 06-December 10

Re: Exit Code 1 and forward declarations

Posted 02 January 2011 - 05:31 PM

I changed them all to void, because that does make sense, Janotte, and I've always written them as int functions haha you can tell im still fairly new to c++. I changed them all to void and still get the error code, which is what I need to fix. And I don't have different files because, number one, I dont know how to define functions in other files. I know to use header files, but every time i've tried writing one, it never works and wont read the functions from file. Is there perhaps a tutorial on here somewhere I can read? I won't make you explain it to me haha
But yeah I just can't figure out that darned exit code
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1