Menu

Having trouble with a header file i created

kip2847
2007-10-14
2012-09-26
  • kip2847

    kip2847 - 2007-10-14

    i keep getting an error for some reason
    Am using c++ 4.9.9.2
    here is my complie log:
    Compiler: Default compiler
    Building Makefile: "C:\Dev-Cpp\Makefile.win"
    Executing make...
    make.exe -f "C:\Dev-Cpp\Makefile.win" all
    g++.exe -c inventorymain.cpp -o inventorymain.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include"

    In file included from inventorymain.cpp:5:
    funct: In function void input_sku()': funct:7: error:sku' undeclared (first use this function)

    funct:7: error: (Each undeclared identifier is reported only once for each function it appears in.)
    funct:10: error: cout' undeclared (first use this function) funct:17: error:cin' undeclared (first use this function)

    inventorymain.cpp: At global scope:
    inventorymain.cpp:6: error: `define' does not name a type

    inventorymain.cpp: In function int main(int, char**)': inventorymain.cpp:16: error:cout' undeclared (first use this function)
    inventorymain.cpp:20: error: `cin' undeclared (first use this function)

    make.exe: *** [inventorymain.o] Error 1

    Execution terminated

    here is my program:
    [main.cpp]

    include <cstdlib>

    include <iostream>

    include <limits>

    include <ios>

    include "funct.h"

    double sku[1500];
    using namespace std;

    int main(int argc, char argv[])
    {
    int choice =0;
    while (choice != 8 )
    {
    cout << "\t Please Choose an Option from the List: \n";
    cout << "\t \t 1 - Scan in SKU. \n" ;
    cout << "\t \t 2 - Compile Scanned items. \n" ;
    cout << "\t \t 8 - Quit Program. \n" ;
    cin >> choice;
    if(cin.fail())
    {
    system("cls");
    cin.clear();
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    cout << " \t \t
    Please enter a valid character* \n";
    }
    if( choice >= 9 || choice < 0)
    {
    system ("cls");
    cout << "This is not one of the options \n";
    }
    if (choice == 1)
    input_sku();
    }
    system("PAUSE");
    return EXIT_SUCCESS;
    }

    [funct.h]

    void input_sku ()
    {
    system("cls");
    int i= 0;
    sku[i] =0;
    while ( i < 1499 && sku[i] != 9999)
    {
    cout << "Please Enter Sku of items: \nWhen Done Enter 9999: \n";
    if (sku[i] < 999999999) // set range of sku
    {
    cout << "Plese Enter a Sku in Range \n ";
    }
    else
    ++i;
    cin >> sku [i];
    system ("cls");
    }
    }

     
    • Osito

      Osito - 2007-10-14

      It's kind of strange to put functions in a header file, but I guess it's not illegal. I would consider it bad practice, but I'm no expert. Regardless, the function in the header file can't access a variable that is defined after the header file is included. Likewise, the function can't assume namespace std is being used if your "using namespace std" line occurs after the header file is included.

       
    • kip2847

      kip2847 - 2007-10-14

      um i dont understand ur answer... mind clearifying?

       
      • Osito

        Osito - 2007-10-14

        Sorry, not sure how to make that more clear. Maybe someone else can help.

         
    • Kurgusov

      Kurgusov - 2007-10-14

      Hi kip...

      I think what you'd need to do is declare sku [ 1500 ] as extern to your header file ( ideally this would give it scope to your headers .cpp file),

      so near the top of your header file add

      extern double sku [ 1500 ] ;

      outside of any function.

      This would give that headers associated source file access to sku[].

      Do you have a .cpp file named the same as the header?

      eg aheader.h and also aheader.cpp ?

      Also have you included iostream in your header, giving scope to std::cout and all.

       
    • Kurgusov

      Kurgusov - 2007-10-14

      multiple files caused me soo much grief until Clifford put my wrongs to rights.

      Its really very easy and goes like this....

      an example...

      ifndef AHEADER_H
      define AHEADER_H

      //put function prototypes here
      //put external variables here as in extern int avariable ;
      //#include other header files whos functions/variables need to be used within this file
      //code class's/structure's here

      endif

      now you create a new file and save it as AHEADER.cpp

      Inside this source file you define the functions/methods and assign values to your variables declared in the files header.

      eg...

      //header

      int sum ;

      //headers .cpp file

      sum = 0 ;

      I hope that helps a bit, it took me ages to get multiple files working together.

       
    • Anonymous

      Anonymous - 2007-10-14

      Header file inclusion is not any kind of magic, it simply replaced the #include with the content of teh header file before compilation, so you have

      include "funct.h"

      double sku[1500];
      using namespace std;

      but funct.h uses sku[]... before it is declared in main.h. It also uses std:: symbols without scope resolution.

      Furthermore there is something very strange about this build. You talk about files "main.cpp" and "funct.h", but the log refers to "inventorymain.cpp" and "funct" (with no extension). And you have teh error message:
      inventorymain.cpp: At global scope:
      inventorymain.cpp:6: error: `define' does not name a type

      ... but there is no symbol 'define' in this code - certainly not on line 6. You really do have to post the code to which the log refers!

      Anyway, that is why you get the errors, but the code is an abuse of the header file mechanism. Header files should include declarative (in C++ inline) code only - no function definitions. Funct.h should merely contain:


      if !defined FUNCT_H

      define FUNCT_H

      extern double sku[]
      extern void input_sku() ;

      endif


      Then you have an additional file funct.cpp


      include <cstdlib>

      include <iostream>

      include "funct.h"

      using namespace std;

      void input_sku()
      {
      ...
      }


      You then need to use Dev-C++ project tool (always a good idea anyway) - File->New->Project, so that you can add multiple sources to your build.

      There are all sorts of other problems with this, but that is the main issue. You should not put your project in c:\dev-cpp\ as you have done. For starters it is 'polluting' the installation. Projects should have their own folder - what will happen when you create a second project? It will screw up the build of this one - perhaps that's how come the log and the code do not match!? Never create projects under c:\dev-cpp\ or a subfolder of that in any case, there is a bug in Dev-C++ that sometimes causes builds to fail when you do that.

      Your input loop never allows you to enter a value for sku[4999] (the last element) - was that intentional?

      Finally let's not get into the rights and wrongs of global data at the moment, but currently sku is declared in main.cpp, but never used. It could be statically declared in funct.cpp, where all functions that access it could reside. This 'data hiding' approach is better done in C++ using a class.

      Clifford.

       
    • Kurgusov

      Kurgusov - 2007-10-14

      2 more things since Im lonely and its sunday.

      its #ifndef...#define...

      remember to include the hash sign.

      and a great tip (i think)..

      whenever you overcome a problem write down the error message the compiler gives and also the solution,
      very soon youll have a manual you can sell off for profit.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.