Jump to content

  • Log In with Google      Sign In   
  • Create Account


C++ Functions run when I don't want them to


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
16 replies to this topic

#1 Cryusaki   Members   -  Reputation: 460

Like
0Likes
Like

Posted 02 February 2014 - 11:19 AM

I want to make a function in main that will not run however just making the function causes it to run

I tried prototyping the function but I am using object that are made and in main and that is out of scope
That goes the same for above main, as the variables/objects inside were defined in main
How do I stop the program from running the function without calling it when I create it?



Sponsor:

#2 dejaime   Crossbones+   -  Reputation: 3968

Like
3Likes
Like

Posted 02 February 2014 - 11:23 AM

Could you post your code?

I have never seen a function running without being called. The code would help us understand.



#3 Lactose!   GDNet+   -  Reputation: 3141

Like
6Likes
Like

Posted 02 February 2014 - 11:35 AM

Additionally, try putting a breakpoint in the function and look through the call stack to see why the function is called.

I really doubt it's being run without being called, but it might be a more obscure call that you don't see right away.



#4 Cryusaki   Members   -  Reputation: 460

Like
0Likes
Like

Posted 02 February 2014 - 12:00 PM

My program has SFML so I am not going to post that code but here is simple version of what I mean

#include <iostream>

using namespace std;

int main()
{
    void function();
    {
        cout << "Hello world!" << endl;
    }

    return 0;
}

The program will say Hello World

but I want it to only run when i call the function later on

How do I stop that?



#5 Paradigm Shifter   Crossbones+   -  Reputation: 5230

Like
15Likes
Like

Posted 02 February 2014 - 12:03 PM

You can't create local functions in C++. (Although lambdas are similar, but that's not what you are trying to do so forget about that)

 

void function();

 

just declares the function prototype (the semicolon at the end gives it away).

 

I think you are confused...


"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#6 Paradigm Shifter   Crossbones+   -  Reputation: 5230

Like
10Likes
Like

Posted 02 February 2014 - 12:04 PM

My program has SFML so I am not going to post that code but here is simple version of what I mean

#include <iostream>

using namespace std;

void function()
{
    cout << "Hello world!" << endl;
}

int main()
{
    return 0;
}

The program will say Hello World

but I want it to only run when i call the function later on

How do I stop that?

 

That's how you prevent it anyway.


Edited by Paradigm Shifter, 02 February 2014 - 12:05 PM.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#7 Lactose!   GDNet+   -  Reputation: 3141

Like
6Likes
Like

Posted 02 February 2014 - 12:07 PM

Alternatively:

#include <iostream>
 
void myFunction(); //Declaration, allows main function to call this if it wants to, even though definition isn't here.
 
using namespace std;
 
int main()
{
 
    return 0;
}
 
void myFunction() //Definition
{
    cout << "Hello world!" << endl;
}


#8 Washu   Senior Moderators   -  Reputation: 4658

Like
9Likes
Like

Posted 02 February 2014 - 04:23 PM

My program has SFML so I am not going to post that code but here is simple version of what I mean

#include <iostream>

using namespace std;

int main()
{
    void function();
    {
        cout << "Hello world!" << endl;
    }

    return 0;
}

The program will say Hello Worldbut I want it to only run when i call the function later onHow do I stop that?

That's not a function. That's a forward declaration of a function followed by some random code surrounded by a scope block

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
ScapeCode - Blog | SlimDX


#9 dejaime   Crossbones+   -  Reputation: 3968

Like
4Likes
Like

Posted 02 February 2014 - 04:31 PM

My program has SFML so I am not going to post that code but here is simple version of what I mean

#include <iostream>

using namespace std;

int main()
{
    void function();
    {
        cout << "Hello world!" << endl;
    }

    return 0;
}

The program will say Hello World
but I want it to only run when i call the function later on
How do I stop that?

 

I see the problem, as previously noted by Paradigm Shifter and Washu, you're not coding a "function inside main".
What you are doing is declaring a function:
 

void function(); // Notice that ";", it marks the end of the function declaration

 
 
and then adding "Hello World!" to cout.

{
    cout << "Hello World!" << endl;
}

the { and } are just block separators, that you can use to limit scopes, between some other things.
So the function and the { cout } aren't related by any means.


Edited by dejaime, 02 February 2014 - 06:22 PM.


#10 Cryusaki   Members   -  Reputation: 460

Like
0Likes
Like

Posted 02 February 2014 - 05:39 PM

I see my issue now, I haven't coded in a long time but I can't believe I made such a basic mistake

Thanks everyone but I have a new issue related to my original issue

How do I make a function that can work with a variable declared in the main?

#include <iostream>

using namespace std;

void function();

int main()
{
    int x = 5;

    return 0;
}

void function()
{
        cout << x << endl;
}

I get an error at second last line saying x is out of scope, I understand why I just don't know how to solve this



#11 Washu   Senior Moderators   -  Reputation: 4658

Like
8Likes
Like

Posted 02 February 2014 - 05:43 PM

You need to either pass in the variable (good), or use a global (bad).


In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
ScapeCode - Blog | SlimDX


#12 Lactose!   GDNet+   -  Reputation: 3141

Like
6Likes
Like

Posted 02 February 2014 - 05:47 PM

#include <iostream>
 
using namespace std;
 
 
void function(int x);
 
int main()
{
    int x = 5;
    function(x);
    function(18); //Different example, using value instead of variable.
 
    return 0;
}
 
void function(int x)
{
    cout << x << endl;
}


#13 BeerNutts   Crossbones+   -  Reputation: 2756

Like
10Likes
Like

Posted 03 February 2014 - 12:42 PM

I see my issue now, I haven't coded in a long time but I can't believe I made such a basic mistake

Thanks everyone but I have a new issue related to my original issue

How do I make a function that can work with a variable declared in the main?

#include <iostream>

using namespace std;

void function();

int main()
{
    int x = 5;

    return 0;
}

void function()
{
        cout << x << endl;
}

I get an error at second last line saying x is out of scope, I understand why I just don't know how to solve this

 

The answer is find a good beginner's C++ tutorial, and start over.  You've got a lot to learn, and it will be easier on you (and us) if you take a little time to understand the basics.


My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

#14 minibutmany   Members   -  Reputation: 1529

Like
0Likes
Like

Posted 03 February 2014 - 04:38 PM


I try not to use classes in C++ because of how functions are called by themself. C style object orientation is more flexible using structs, virtual function tables and separate methods.

 

Before it was called C++, it was called "C with Classes".


Stay gold, Pony Boy.

#15 dejaime   Crossbones+   -  Reputation: 3968

Like
2Likes
Like

Posted 03 February 2014 - 04:44 PM

Before it was called C++, it was called "C with Classes".

I used to be called a baby. That was 20+ years ago.

#16 Khatharr   Crossbones+   -  Reputation: 2934

Like
0Likes
Like

Posted 06 February 2014 - 06:46 PM

Wow. It was a pretty ignorant comment, but -21 is pretty harsh for the beginner's forum. Let's calm down a little, lol.


void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

#17 dejaime   Crossbones+   -  Reputation: 3968

Like
3Likes
Like

Posted 06 February 2014 - 07:03 PM

Wow. It was a pretty ignorant comment, but -21 is pretty harsh for the beginner's forum. Let's calm down a little, lol.

Actually, it is not only ignorant, but a quite misleading comment, and targeted at an absolute beginner, what makes matters worse. Given that the up vote hover message is: This post provides useful information and demonstrates knowledge of the subject, voting it up would make absolutely no sense.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS