Solved

Attempt of a basic header File.

Posted on 2004-08-10
9
336 Views
Last Modified: 2010-04-01
Hi all,
I'm rather new to C++, and that's why I've made some stupid mistakes (i'm sure) and can't spot them.
I've coded a very simple header file that can be used as an algorithm for Traffic Lights.

This is the header file below:

//  traffl.h
//  # A basic Traffic Light Algorithm.
//------------------------------------

#ifndef  _traffl_
#define  _traffl_

#define  BEFORE_RED          2
#define  BEFORE_AMBER        2
#define  BEFORE_GREEN        3
#define  GREEN_DURATION      12

#include <iostream>
#include <windows>
using namespace std;

class traffl
{
    public:
        void lightControl ( int *l, bool *end )
        {
            // int e_c  =  number of elements;
            e_c = sizeof(*l);

            while ( !(*end) ) {
                for ( int a = 0; a < e_c; a++ ) {
                    for ( int b = 0; b < e_c; c++ ) *l[b] = 0;
                    Sleep( 1000 * BEFORE_AMBER   );
                    *l[a] = 1;
                    Sleep( 1000 * BEFORE_GREEN   );
                    *l[a] = 2;
                    Sleep( 1000 * GREEN_DURATION );
                    *l[a] = 1;
                    Sleep( 1000 * BEFORE_RED     );
                }
            }


        }
};

#endif


The above header file is saved as: traffl.h  and saved in the same directory as the below program (that attempts to use it):

//  t_tl.cpp
//  # A prog to make use of the traffl class.
//------------------------------------------------

#include <iostream>
#include "traffl.h"
using namespace std;

void traffl::lightControl( int l[], bool end );    // Prototype.

int main()
{
    int ar[4] = { 0, 0, 0, 0 };

    bool e = false;
    traffl tl;
    tl.lightControl( ar, e );

    // Process data (altered by pointers in traffl.h) here.

    Sleep( 5000 );
    e = true;

    return 0;
}


However, when I compile the t_tl.cpp program, I get one error, which is:
Line 6: c:\docume~1\admin\mydocu~1\algori~1\traffi~1\t_tl.cpp
c:\docume~1\admin\mydocu~1\algori~1\traffi~1\traffl.h:14: windows: No such file or directory

I'm guessing it cannot find the header file I'm trying to import...? I have also copied and pasted the header file (traffl.h into my compilers library, (along with iostream.h, etc..) in the PATH directory, right?).

Any ideas what's going on here??

Thanks in advance!
[r.D]
0
Comment
Question by:DrWarezz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
9 Comments
 
LVL 30

Accepted Solution

by:
Axter earned 40 total points
ID: 11766564
Add the .h extension

#include <windows.h>
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 40 total points
ID: 11766578
>>c:\docume~1\admin\mydocu~1\algori~1\traffi~1\traffl.h:14: windows: No such file or directory

That should be

#include <windows.h>

Furthermore,

        void lightControl ( int *l, bool *end )
       {
           // int e_c  =  number of elements;
           e_c = sizeof(*l);

           while ( !(*end) ) {
               for ( int a = 0; a < e_c; a++ ) {
                   for ( int b = 0; b < e_c; c++ ) *l[b] = 0;
                   Sleep( 1000 * BEFORE_AMBER   );
                   *l[a] = 1;
                   Sleep( 1000 * BEFORE_GREEN   );
                   *l[a] = 2;
                   Sleep( 1000 * GREEN_DURATION );
                   *l[a] = 1;
                   Sleep( 1000 * BEFORE_RED     );
               }
           }


       }
is very unlikely to work, since

 e_c = sizeof(*l);

will always return '4' and 'end' will never change.
0
 
LVL 30

Expert Comment

by:Axter
ID: 11766587
Most header files have a .h extension.
The STD header files are the main exception to this rule. (<iostream> <string>, <map>, <vector>, etc.....).

All the windows header files have a *.h extension.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Author Comment

by:DrWarezz
ID: 11766681
Hi,

>> "e_c = sizeof(*l);      \n will always return '4' and 'end' will never change"
Why exactly would "int e_c = sizeof(*l);" always return 4?
Also, why would 'end' never change?  I assumed that because it was pointing to the 'end' variable in the t_tl.cpp program, then when the t_tl.cpp changes the 'end' variable, then it would update it in the header class.
Would I need to use a reference instead of a pointer? Or, can what I'm trying to do simply not be done?

Thanks so far,
[r.D]
0
 
LVL 86

Expert Comment

by:jkr
ID: 11766733
>>Why exactly would "int e_c = sizeof(*l);" always return 4?

Because it's the size of an 'int' you are evaluating.

>>Also, why would 'end' never change?

Your processing is not parallel. The control will return to 'main()' only after 'lightControl()' has terminated.
0
 
LVL 9

Author Comment

by:DrWarezz
ID: 11766789
:o\   Oh.

Is there anyway that I can allow the main() function to control how long the "lightControl()" runs for?
Hmm... Would I have to rely on threads for this?  If so, do you know of a decent "Thread tutorial" for C++ ?
I can do them fine in Java... But, this C++ is soo much harder. lol.

Thanks,
[r.D]
0
 
LVL 30

Expert Comment

by:Axter
ID: 11766836
Why do you want to do threads?
Is this for homework?

If you need the size of the array, you should pass it as an additional argument.

void lightControl ( int *l, int SizeOf_I, bool *end )

Example usuage:

tl.lightControl( ar, sizeof(ar)/sizeof(ar[0]), e );

0
 
LVL 86

Expert Comment

by:jkr
ID: 11766852
Hmm, I'd recommend http://msdn.microsoft.com/library/en-us/dndllpro/html/msdn_threads.asp ("Multithreading for Rookies")
0
 
LVL 9

Author Comment

by:DrWarezz
ID: 11767220
Hmm.. That was really stupid what I done; To call the method, then expect control of it AFTER it's finished (assuming it was running as a seperate process (ie, a thread)).
You see, thinking back to my Java days :P lol, when I wanted the ability to run a process (by calling another method), then be able to end it at any point, I would create the process as a thread, then kill it when I wanted to.
For some reason, I assumed that I could call the method "lightControl( ... )", and that the main() function would continue to run, WHILE this lightControl() was also.. I know this wouldn't happen, yet I, for some reason attempted it!! lol :o\ Damn... how stupid! lol.

I also get what you mean by the "would always return "4"" thing now. lol.. It's ALL making sense!! :P

Thanks,
[r.D]

I shall look at the thread tutorial, recommended by "jkr" now. Thanks.
0

Featured Post

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question