Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Attempt of a basic header File.

Posted on 2004-08-10
9
Medium Priority
?
338 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
  • 3
  • 3
  • 3
9 Comments
 
LVL 30

Accepted Solution

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

#include <windows.h>
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 160 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

916 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