A certain global isn't "recognized" inside of a certain function

Time-appropriate greetings!

I have ran into a weird issue:
A global variable isn't "recognized" by a certain function.
It works everywhere else, in all other functions, in the "main" part, everything is fine.
But for some reason, the compiler gives me the error:
"ActualTime" does not name a type.

I tried to rename it to something else, but that didn't help either.

No other functions have any issues with reading / writing to or from this or other global variables.

1
2
3
4
5
6
7
8
9
10
11
12

int ActualTime = 0;  //Current time in seconds, starts at 0


//The problematic function:

bool CheckTime(int SomeNumber)
  {
    bool ReturnValue;
    ActualTime = SimulationStartTimeSS + SimulationStartTimeMM * 60 + SimulationStartTimeHH * 60 * 60;
  //do some other stuff ...
  return ReturnValue;


What could be causing this?
It just does not make any sense ...
Hello leander g,

First start with enough code that can be compiled and tested. And enough code that will duplicate the problem. What you have not shown could be where the problem starts.

Second it is always best to post the complete error message generated by the compiler. It is quite possible that your interpretation of the error message may not be correct.

When trying to duplicate your error I ended up with this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int ActualTime = 0;  //Current time in seconds, starts at 0
int SimulationStartTimeSS{}, SimulationStartTimeMM{}, SimulationStartTimeHH{};

//The problematic function:

bool CheckTime(int SomeNumber)
{
    bool ReturnValue{};

    ActualTime = SimulationStartTimeSS + SimulationStartTimeMM * 60 + SimulationStartTimeHH * 60 * 60;

    //do some other stuff ...

    return ReturnValue;
}

int main()
{ }

Still this did not duplicate your problem, but I can not see what is missing.

In line 10 "ActualTime" was not a problem, but the other 3 variables gave the error as undefined. That is why I added line 2 for now.

As a note neither line 1 or line 2 should be defined as global variables because any line of code that follows them can change the value(s) and try to figure out where that happened could take a very long time, (hours or even days).

It would be better to put lines 1 and 2 in "main" and pass them to the function(s) that need them.

The last part I noticed is: bool ReturnValue;. You have defined the variable, but it contains a garbage value. Not sure if "//do some other stuff ..." would give this variable a proper value or not I had to initialize the variable so the return statement would not become an error.

If you can post the entire code or people can only guess at what might be wrong.

Andy
I really feel stupid right now ...

While I was selecting that whole function to copy and paste it here, I noticed someting:

I had left a } somewhere in the function where an if() used to be ...

I deleted that } and now it compiles just fine, no problems ..


I guess I will double check for that sort of stuff from now on before posting here ...

Sorry for wasting your time .....

(And yes, the compiler also said that the function had no return, because it thought that the function "ended" at that stray } )
Hello leander g,

No worries.

It is always the little things that cause the biggest problems.


(And yes, the compiler also said that the function had no return, because it thought that the function "ended" at that stray } )


That was not my point. The point is that "ReturnValue" did not have a valid value based on the code provided. Actually the original code did not have a closing }, but I did add one.

Andy
I know, I just copy/pasted the parts of that fuction here that had that "problematic variable" in it, not the whole function.

I would have had to change a few things before posting it (because it is a mess) and I was too lazy to do that, so I just left most of the "actual stuff" out ...

But it is basically just a function that checks if if an event has occured a certain time ago and based on that returns true or false.
Nothing too complicated.

That's what really irritated me. The fact that something simple wouldn't "just work".
Topic archived. No new replies allowed.