5 Replies - 32276 Views - Last Post: 20 August 2011 - 06:54 PM Rate Topic: -----

#1 Alexera   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 20-August 11

error function-definition is not allowed here before'{' token.

Posted 20 August 2011 - 11:36 AM

When I write my code into my compiler I get thhis error

error: a function-definition is not allowed here before'{' token.

But I've looked through my code and I can't fin anything wrong with the brackets?\

Can anyone tell me what I'm missing?

Code:
#include <iostream>

using namespace std;

int main()
{

  void check (int x)
{

    if (x < 0)
    x = -x
    cout << "The value if " << x << endl;
}

   void check (float x)
{
    if (x < 0)
    x = -x
    cout << "The value if " << x << endl;
}

    void check (double x)
{

    if (x < 0)
    x = -x
    cout << "The value if " << x << endl;
}
    cin.get ();
    return 0;
}


MOD EDIT: Added code tags. When posting code...USE COE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 20 August 2011 - 12:45 PM


Is This A Good Question/Topic? 0
  • +

Replies To: error function-definition is not allowed here before'{' token.

#2 Bench   User is offline

  • D.I.C Lover
  • member icon

Reputation: 945
  • View blog
  • Posts: 2,464
  • Joined: 20-August 07

Re: error function-definition is not allowed here before'{' token.

Posted 20 August 2011 - 11:51 AM

Quote

int main()
{

void check (int x)
{

Here it looks like you're trying to define a function inside another function (which isn't allowed!). defining and/or declaring a function works differently to actually calling the function.

I suggest you have a read through the function tutorials here, which explains all of this in a bit more detail:
http://www.dreaminco...t-i-the-basics/
http://www.dreaminco...ing-and-arrays/

And of course, for even more on functions consult your favourite book/C++ reference/tutorial/google/etc.

This post has been edited by Bench: 20 August 2011 - 11:55 AM

Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg   User is offline

  • member icon

Reputation: 5678
  • View blog
  • Posts: 17,446
  • Joined: 25-December 09

Re: error function-definition is not allowed here before'{' token.

Posted 20 August 2011 - 11:52 AM

Quote

Can anyone tell me what I'm missing?

Code tags!!!

You can not declare a function inside another function. All of your functions are being implemented inside your main function. See this link: Functions I.


Jim
Was This Post Helpful? 1
  • +
  • -

#4 hulla   User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 733
  • Joined: 05-March 11

Re: error function-definition is not allowed here before'{' token.

Posted 20 August 2011 - 04:15 PM

View PostAlexera, on 21 August 2011 - 02:36 AM, said:

Code:
  void check (int x)
{

    if (x < 0)
    x = -x
    cout << "The value if " << x << endl;
}

   void check (float x)
{
    if (x < 0)
    x = -x
    cout << "The value if " << x << endl;
}

    void check (double x)
{

    if (x < 0)
    x = -x
    cout << "The value if " << x << endl;
}

You're missing braces for your "if" statements dude . . . To read up on if statements, If Statements (learncpp.com)

Edit: Typo . . .

This post has been edited by hulla: 20 August 2011 - 04:16 PM

Was This Post Helpful? 0
  • +
  • -

#5 Alexera   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 20-August 11

Re: error function-definition is not allowed here before'{' token.

Posted 20 August 2011 - 06:29 PM

Thank you all so much for your help, from all your information I've been able to fix all but one error.

This is the error I get now:

expected initializer before 'int'

I read the links you gave me, and I tried void within the int parenthesis and some other changes, but I'm still getting this error.
Any more help would be really appreciated.

#include <iostream>
using namespace std;

  void check (int x)
    int main()
{
    if (x < 0)
{
    x = -x;
    cout << "The value is " << x << endl;
}
    return 0;
}
   void check (float x)
    int main()
{
    if (x < 0)
    {
    x = -x;
    cout << "The value is " << x << endl;
    }
    return 0;
}
    void check (double x)
    int main()
{

    if (x < 0)
    {
    x = -x;
    cout << "The value is " << x << endl;
    }
    cin.get ();
    return 0;
}




Sorry I didn't put the code tags last time.

This post has been edited by Alexera: 20 August 2011 - 06:30 PM

Was This Post Helpful? 0
  • +
  • -

#6 PlasticineGuy   User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: error function-definition is not allowed here before'{' token.

Posted 20 August 2011 - 06:54 PM

You completely ignored what we said. You cannot put functions inside other functions!
#include <iostream>
using namespace std;
void check (int x)
{
    if (x < 0) {
        x = -x;
    }
    cout << "The value is " << x << endl;
}
// overloads here
int main ()
{
    cin.get():
    return 0;
}

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1