Get .cpp and .h files to communicate...

This is a discussion on Get .cpp and .h files to communicate... within the C++ Programming forums, part of the General Programming Boards category; I'm trying to figure out how to get my .cpp file to #include other .cpp and .h files. Here is ...

  1. #1
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204

    Thumbs down Get .cpp and .h files to communicate...

    I'm trying to figure out how to get my .cpp file to #include other .cpp and .h files. Here is a little test I setup:

    main.cpp:
    Code:
    #include "stdafx.h"
    #include "test.cpp"
    
    int main()
    {
        tester temp("Thins is the string");
    
    	cout<<temp.GetString();
    	
    	getche();
    
        return 0;
    }
    test.cpp:
    Code:
    #include "test.h"
    
    string tester::GetString()
    {
    	return teststring;
    };
    
    tester::tester(string newstring)
    {
    	teststring = newstring;
    };
    test.h:
    Code:
    #include <iostream>
    #include <conio.h>
    
    #ifndef _TEST_H
    #define _TEST_H
    
    using namespace std;
    
    class tester
    {
    public:
    	tester(string);
    	string GetString();
    private:
    	string teststring;
    };
    
    #endif
    I'm getting a couple of errors saying stuff like:
    '#include "test.h"': skipped when looking for precompiled header use
    binary '<<' : no operator found which takes a right-hand operand of type 'std::string'...

    Thanks.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,650
    1) You need to turn off pre-compiled headers in the project settings.
    2) You typically do not include CPP files. main.cpp should include "test.h".
    3) test.h has no dependencies on conio.h and shouldn't include it.
    4) test.h depends on <string> and should include it
    5) Never put a global "using namespace" in a header - that just forces anyone who includes you to "use" it as well - which is presumptuous.
    6) main.cpp depends on "test.h", <iostream> and <conio.h>. That "stdafx.h" can be deleted and removed from the project once pre-compiled headers have been turned off.

    gg

  3. #3
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    Quote Originally Posted by Codeplug View Post
    2) You typically do not include CPP files. main.cpp should include "test.h".
    Then how does the rest of your project access the contents of the .cpp files?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You include all cpp files in your project or make file or on your command line so that they each get compiled separately and linked together.

    If you're using VC++ just add the cpp file as a source file to the project (you might as well add the headers, too but that doesn't matter for the build.

  5. #5
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    Alright, I've made some changes but I've got these two new errors:
    "int variable" (?variable@@3HA) already defined in main.obj
    one or more multiply defined symbols found

    If you need the new code for more information then I will add it.

    Thanks

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Code would help. It sounds like you've got a global variable defined in the header file. Global variables are generally bad practice anyway, but if you're going to have one you have to define it in the source file.

  7. #7
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    Aha! I got it working now, yes there was a global variable in there.
    Thanks Daved, your nothing short of a marvel.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. #include header files or .cpp files?
    By DoctorX in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2006, 11:21 AM
  2. .cpp and .h files - organization
    By ChadJohnson in forum C++ Programming
    Replies: 4
    Last Post: 01-12-2006, 10:40 PM
  3. .h files: "constant already defined"
    By wakeup in forum C++ Programming
    Replies: 11
    Last Post: 11-22-2005, 04:31 AM
  4. placement of (.h) and (.cpp) files..!?
    By matheo917 in forum C++ Programming
    Replies: 3
    Last Post: 02-22-2003, 05:37 PM
  5. Class files (.h and .cpp
    By Todd in forum C++ Programming
    Replies: 7
    Last Post: 02-14-2002, 02:07 PM

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