Thread: Visual Studio 2005 (C++)

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    41

    Visual Studio 2005 (C++)

    I am trying to do a multiple file program. I have class and function declarations in header files, and their corresponding definitions in .cpp source files. If I use the #include directive explicity for each filename, my project will compile just fine.

    Code:
    using namespace std;
    #include fraction.h
    #include func.h
    #include fraction.cpp
    #include func.cpp
    
    main()
    {
    //bla bla bla bla
    //
    }
    If you know Visual Studio 2005, you might be able to help with this, for this does NOT work

    Header Files
    -fraction.h
    -func.h
    Reference Files
    Source Files
    -Program.cpp (main source file)
    -fraction.cpp
    -func.cpp

    Yes, I did tell the compiler to look in the project directory for each of those files
    in Tools-->Options-->Projects and Solutions-->VC++ Directiories. What else must I do to make the compiler accept these files? If I try to compile it the way shown just above here, I get a vomit of error messages.

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Do you have a good reason for including source files? What sort of errors do you get? You're saying it does not work when your solution explorer is laid out like this?

    Code:
    [Header Files]
    -fraction.h
    -func.h
    [Resource Files]
    [Source Files]
    -Program.cpp
    -fraction.cpp
    -func.cpp

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    41
    That is precisely what I am saying.

    If I include just the header files and keep the function definitions in the program.cpp, things will work. The errors that I get involve arguments to the functions in the included .cpp files. I use istream and ostream objects, as well as a class object I created. My reason for doing this is becuase it is part of an assignment for a class=P. I have a feeling it has to do with how to manipulate the compiler menus......the code is all good, it complies if I throw ALL of it in the program.cpp file.

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    The only time you might include source files is if you're using templates, and then it should be at the bottom of your header file. There's been plenty of examples of how to split your project into multiple source files, but here you go:
    Code:
    //program.cpp
    #include "func.hpp" //hpp extension might be preferable
    
    int main()
    {
    //...
    }
    Code:
    //func.hpp
    #ifndef FUNC_H_ //arbitrary macro name for include guard
    #define FUNC_H_
    
    //function prototypes and inline and template function definitions in headers
    void Func(); 
    inline void Func2()
    {
      //...
    }
    template <class T>
    void Func3()
    {
      //...
    }
    
    #endif
    Code:
    //func.cpp
    #include "func.hpp"
    
    void Func()
    {
     //...
    }
    Edit: And if this doesn't help, I think we might need a more specific/complete example from you.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I get a vomit of error messages.
    You can post them, or at least the first few.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM