how can I create a header file ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • scienceman88
    New Member
    • Aug 2009
    • 85

    #16
    Code:
    #ifndef RODDYH
    #define RODDYH
     
    float log_x(unsigned long long a, unsigned long long b);
    float root_x(unsigned long long c,unsigned long long d);
     
    #endif
    and

    Code:
    #include <math.h>
    #include <stdlib.h>
    #include "roddy.h"
    
    float log_x(unsigned long long a, unsigned long long b)
    {
    float c = 1/b;
    return pow(a,c);
    }
    Warning 1 warning C4244: 'initializing' : conversion from 'unsigned __int64' to 'float', possible loss of data c:\users\roddy\ documents\visua l studio 2008\projects\r oddy\roddy\rodd y.c 7 roddy

    Warning 2 warning C4244: 'function' : conversion from 'unsigned __int64' to 'double', possible loss of data c:\users\roddy\ documents\visua l studio 2008\projects\r oddy\roddy\rodd y.c 8 roddy

    Warning 3 warning C4244: 'return' : conversion from 'double' to 'float', possible loss of data c:\users\roddy\ documents\visua l studio 2008\projects\r oddy\roddy\rodd y.c 8 roddy

    Error 4 error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStar tup MSVCRTD.lib roddy

    Error 5 fatal error LNK1120: 1 unresolved externals C:\Users\roddy\ Documents\Visua l Studio 2008\Projects\r oddy\Debug\rodd y.exe roddy

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #17
      Warning 1,2 accessing a and b
      On line 7 and 8 of you cpp file you are mixing integer and floating point types in your arithmetic. The compiler automatically converts the integer types to floating point types but issues a warning when doings so.

      Warning 3 returning pow
      pow returns double but you return float, another automatic convertion is done but a warning is issued.

      The function probably does not do what you expect because 1/b when b is an integer type is unlikely to do what you think it does. That is because all the components of 1/b are integers so it is done in integer arithmetic, that is the result is given as an integer. So you might think the result of say 1/2 would be 0.5 but 0.5 is not an integer, the fractional part of 0.5 is dropped to give a result of 0. In fact under integer arithmetic 1/b can only have 4 results

      b=0 gives divied by zero error
      b = 1 gives 1/1 = 1
      b = -1 gives 1/-1 = -1
      b = any other value 1/b is fractional and the result is 0.

      Error 4 - you are aware that you need to write a main function as the entry point of your program are you?

      Error 5 - you can ignore it is just re-reporting error 4 as a summary.

      Comment

      • scienceman88
        New Member
        • Aug 2009
        • 85

        #18
        working with .c file not .cpp, header never used main that I have ever seen.

        Comment

        • scienceman88
          New Member
          • Aug 2009
          • 85

          #19
          Code:
          #ifndef RODDYH
          #define RODDYH
           
          double log_x(double a, double b);
           
          #endif
          Code:
          #include <math.h>
          #include <stdlib.h>
          #include "roddy.h"
          
          void main();
          double log_x(double a, double b)
          {
          double c = 1.0/b;
          return pow(a,c);
          }
          still got both errors

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #20
            First main returns int it should be

            int main()

            Secondly, you have to define a function called main. All you have done is declared one (See Below). The linker expects to find a main function defined in your program it is the program entry point.

            Read entry point

            and main

            You will never find main in any header file because it is not a function you call (normally) so it doesn't need to be declared for you. It is a function that the C start-up code created by the linker calls and it already knows how it is declared, you have to define this function in one of your C files.





            Note From Above Difference between Declaring and Defining
            When you declare something all you do is say it exists somewhere, a function declaration looks like this
            Code:
            void fn(int q);
            When you define something you not only say it exists but also give its actual contents. In the case of a variable that is an instruction to the compiler to actually assign memory to it, for a function you write the block of code that is executed by the function. This is a function definition
            Code:
            void fn(int q)
            {
                while(q-- > 0)
                {
                    putchar('*');
                }
            
                putchar('\n');
            }

            Comment

            • scienceman88
              New Member
              • Aug 2009
              • 85

              #21
              main is in most programs but this is header and function set I usually let dev c++ make main for me

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #22
                main is in ALL or its equivalent if the platform has redefined the entry point, for example WinMain in a Windows program.

                If you are trying to create a function or set of functions that are to be used by including them into a different program then you have created the wrong type of project. You have created a console application project but you should have created a library project if you wanted to write functions without writing a program.

                Comment

                • scienceman88
                  New Member
                  • Aug 2009
                  • 85

                  #23
                  The thing I hate is that I asked a question about header files, got told by you to switch IDE's and to use a console app yet now I'm being told that's wrong. also the only thing I see that says library is class library and I'm not trying to make a class.

                  Comment

                  • Banfa
                    Recognized Expert Expert
                    • Feb 2006
                    • 9067

                    #24
                    I did not tell you to switch IDEs I suggested it might be worth doing as an side to a post in which I was trying to answer your question.

                    We told you to use a console application because up until post 21 you can no indication that you were attempting to create a library, you appear to be attempting to write a program without main or with the wrong project type so we had to guess what project type you required which we not wrong in no small part due to the fact that at no time have you every properly said what it is you were trying to achieve.

                    Class library is not what you want, if you followed the instructions given to you in post #3 and bother to examine the displayed dialogues you would see the options is for creating a DLL (dynamic link library) or a static library.

                    Comment

                    • scienceman88
                      New Member
                      • Aug 2009
                      • 85

                      #25
                      no you know the fact that the question involved the word header is no indication, telling me to consider switching is in a way telling me to so in fact you have told me to switch, I've followed #3 then made the files inside, and surprise I got exactly the same errors!

                      Comment

                      • weaknessforcats
                        Recognized Expert Expert
                        • Mar 2007
                        • 9214

                        #26
                        The compiler is only reporting what it finds. Any errors are in the code. You fix the first error only. Then rebuild. Then fix the first error only and rebuild. Continue this process until you have no errors.

                        It is irrelvant what you think or believe as the compiler is completely impersonal. But until that first error is fixed you will be dead in the water. Pay cloase attention to Banfa as he knows at lot about this stuff.

                        BTW: Header files do not need to be part of a project. It is the #include directive that prompts the preprocessor to locate and include your headers. You need header in a project only if that project is the one that maintains the headers.

                        BTW: It doesn't matter that the file extension is .c or .cpp. That's just a convenience for you. Most IDEs can compile the all the files a C or C++ based on a project setting. The .c for C code or .cpp for C++ code is just a default.

                        Comment

                        • scienceman88
                          New Member
                          • Aug 2009
                          • 85

                          #27
                          fine I'll redefine what I want:

                          1) a header file
                          2) the source to go along with it so that the functions have meaning and can be later included in projects

                          Comment

                          • scienceman88
                            New Member
                            • Aug 2009
                            • 85

                            #28
                            <code removed>

                            like this

                            Comment

                            • Banfa
                              Recognized Expert Expert
                              • Feb 2006
                              • 9067

                              #29
                              Err, sorry I had to remove that code because it contained a copyright notice.

                              However everything you need to achieve what you want in post #27 has already been posted here.

                              Post #2, weaknessforcats tells you how to create a header file,
                              Post #3 they tell you how to create a project for that header file and your source code in the IDE you are now using.

                              I have told you that you need to modify the instructions in post #3 to create a library (either static or DLL) instead of a console application, it is just a different radio button on the same applications settings dialogue.


                              Thought out the whole of this thread I confess to being a little perplexed as to why you are making the task of creating a header file, which is a plain text file, so complex.

                              Comment

                              • scienceman88
                                New Member
                                • Aug 2009
                                • 85

                                #30
                                okay I got the dll built but now it's asking me to name an exe to debug I don't have one that uses the new codes how do I do this part.

                                Comment

                                Working...