Thread: Including extra .cpp files visual2008

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    38

    Including extra .cpp files visual2008

    Hi how do you go about including two or more .cpp files in visual express 2008?Have no problem including headers but cant seem to fathom including extra .cpp files.Tried to find it on msdn but just ended up going around in circles....

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Why do you need to include .cpp files? The one special case I can think of when you would include a source file is to include it in a header file where templates are defined.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    I think he is asking how to add new .cpp files to a project in the IDE. I've never used VS 2008, but can't you just add a generic file and name it with a .cpp extension?

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    38
    Yeah sorry didnt make myself clear.

    At the moment I have

    main.cpp
    main.h(function prototype's)
    playerClass.h(my structs and class's)

    and I want to include a playerClass.cpp with the structs and classes are initialised.I have being doing that in main.cpp but it is starting to get unwieldy.

    I have added an extra .cpp file in the manager project(as a source file) but when I go to compile it gives errors that the varibles are un-declared

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Cathalo View Post
    I have added an extra .cpp file in the manager project(as a source file) but when I go to compile it gives errors that the varibles are un-declared
    Sounds like everything is working. Go fix your bugs.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    38
    Quote Originally Posted by brewbuck View Post
    Sounds like everything is working. Go fix your bugs.

    i Have for example int x=10 in playerClass.cpp.If i cout<<x; in main I get the error undeclared var.Main isnt picking up playerClass.cpp at all.

    Will keep at it.Doesnt help that Im off cigarettes 5 days.Nearly hopped the pc off the wall.twice

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Cathalo View Post
    i Have for example int x=10 in playerClass.cpp.If i cout<<x; in main I get the error undeclared var.Main isnt picking up playerClass.cpp at all.

    Will keep at it.Doesnt help that Im off cigarettes 5 days.Nearly hopped the pc off the wall.twice
    That's the way it's supposed to work. Variables aren't related between functions, let alone between files.

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    38
    So what can go in the .cpp file then?

    Will just keep using one cpp file and the headers.Not worth the head ache.And its not as If im programming Microsoft Office suite 2009 or half life 3

    Thanks to all

  9. #9
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    If you're use to other compilers like codeblocks or devcpp where you can include a header file with the prototypes AND the functions then yea visual studio 2008 can be confusing. I know it was for me at first.

    For my projects I use .cpp files for the function 'code' and the .h header files for prototyping the functions. That way VS doesn't complain about having undefined variables/functions. Just include the header file in the main.cpp. So basically in the end you should have;

    -main.cpp
    -main.h
    -playerClass.cpp // all the functions
    -playerClass.h //all the function prototypes

  10. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    38
    Got ye.I think i was trying the impossible

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to better manage large .cpp files
    By 39ster in forum C++ Programming
    Replies: 6
    Last Post: 08-25-2008, 08:24 AM
  2. Using multiple .cpp files for programs
    By DevEight in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2007, 07:56 AM
  3. Class files (.h and .cpp
    By Todd in forum C++ Programming
    Replies: 7
    Last Post: 02-14-2002, 03:07 PM
  4. How do I include .cpp files in my main?
    By MadForIt in forum C++ Programming
    Replies: 8
    Last Post: 02-11-2002, 07:40 PM
  5. Including Files
    By Kinc in forum C++ Programming
    Replies: 1
    Last Post: 01-02-2002, 03:31 PM