need help creating two source files..

This is a discussion on need help creating two source files.. within the C++ Programming forums, part of the General Programming Boards category; ok say heres my code in once source file.. Code: #include <sstream> #include <fstream> #include <iostream> #include <string> #include "stdlib.h" ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    44

    need help creating two source files..

    ok say heres my code in once source file..

    Code:
    #include <sstream> 
    #include <fstream>
    #include <iostream>
    #include <string>
    #include "stdlib.h"
    #define SIZE 50 // size of the array
    
    using namespace std;
    
    class store // defines a class with variables
    {
          public:
                 int custnum;
                 string product;
                 int quantity;
                 string month;
                 string day;
                 string year;
                 int tmpcustnum;
                 int tmpquantity;
                 string tmpmonth;
                 string tmpday;
                 string tmpyear;
                 bool isfirsttime;
    };
    store STORE1[SIZE]; // creates a class
    
    
    void menu()
    {
    .
    .
    }
    
    void period(int num_line, string productname, string month1, string day1, string year1, string month2, string day2, string year2)
    {
    .
    .
    }
    
    int main()
    {
        menu(); // calls menu function
        system("PAUSE");
        return 0;   
    }
    what would i put in the header file.. the menu and period functions use the class..

    this is my header file

    Code:
    #ifndef BANK_H  
    #define BANK_H
    #define SIZE 50
    
    class store // defines a class with variables
    {
          public:
                 int custnum;
                 string product;
                 int quantity;
                 string month;
                 string day;
                 string year;
                 int tmpcustnum;
                 int tmpquantity;
                 string tmpmonth;
                 string tmpday;
                 string tmpyear;
                 bool isfirsttime;
          
    };
    store STORE1[SIZE]; // creates a class
    void menu();
    #endif
    i included my header file and when i try to compile main.cpp
    Code:
    #include <sstream> 
    #include <fstream>
    #include <iostream>
    #include <string>
    #include "bank.h"
    
    int main()
    {
        menu(); // calls menu function
        system("PAUSE");
        return 0;   
    }
    it gives me this error 18 K:\...\bank.h `string' does not name a type


    also how would i do a makefile?

  2. #2
    Registered User
    Join Date
    Oct 2005
    Posts
    44
    any1 plz!! my program is due tomorrow

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Your problem I think is that bank.h dosen't include the 'string' header file where the string class is defined.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    44
    still gives me same error

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    You also need to put using namespace std; in the header file or precede the string type with std::.

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    44
    i revised my header file but now it says menu is undefined..
    Code:
    #define SIZE 50
    #include <sstream> 
    #include <fstream>
    #include <iostream>
    #include <string>
    using namespace std;
    class store // defines a class with variables
    {
          public:
                 int custnum;
                 string product;
                 int quantity;
                 string month;
                 string day;
                 string year;
                 int tmpcustnum;
                 int tmpquantity;
                 string tmpmonth;
                 string tmpday;
                 string tmpyear;
                 bool isfirsttime;
    
    };
    store STORE1[SIZE]; // creates a class
    
    void menu();

  7. #7
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Where is the menu function code. It should be in main.cpp.

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    44
    the menu function is in my other c++ file - i put menu() in main now its giving me errors that my other functions are undeclared since menu calls other functions
    Last edited by loso44x; 11-18-2005 at 01:59 AM.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    Separate all the method definitions into one or more source files (.cpp) however you think they should go. Then, in each source file, look for each function defined or used and each class used, and include the header that declares that function or class.

    Then make sure to compile all the source files and link them together and it should work.

  10. #10
    Registered User
    Join Date
    Oct 2005
    Posts
    44
    1234
    Last edited by loso44x; 11-18-2005 at 01:37 PM.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    Does that mean it works, or are there still errors? Give the full, exact error messages if there are errors.

  12. #12
    Registered User
    Join Date
    Oct 2005
    Posts
    44
    it works i discarded the header file and just includded functions.cpp in main.cpp ... thanks

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Quote Originally Posted by loso44x
    it works i discarded the header file and just includded functions.cpp in main.cpp ... thanks
    uhh ok but there shouldn't be a need to do that. Just remember every cpp file that uses a function must include the header where that function is prototyped. The cpp that defines that function must also include the header file.

  14. #14
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    Also, if their are multiple source files, you may need to compile them separately and then link them together as the last step.
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confusion on header and source files
    By dnguyen1022 in forum C++ Programming
    Replies: 4
    Last Post: 01-17-2009, 02:42 AM
  2. source parsers for creating documentation?
    By m37h0d in forum C++ Programming
    Replies: 2
    Last Post: 12-29-2008, 08:59 AM
  3. Class Inheritance over multiple source files
    By Swarvy in forum C++ Programming
    Replies: 7
    Last Post: 11-11-2008, 09:03 AM
  4. add source files to embedded VC 4.0
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2006, 03:28 AM
  5. header and source files
    By gtriarhos in forum C Programming
    Replies: 3
    Last Post: 10-02-2005, 03:16 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21