Thread: #include

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    13

    #include

    I have three files.

    main.cpp
    webcounter.cpp
    webcounter.h

    I have my class declarations in my webcounter.h file.
    I have my class definitions in my webcounter.cpp file.

    How do I include both files to run in my main.cpp file?

    My professor told us we shouldn't include .cpp files, yet I'm not understanding another way to get both my class definitions and declarations to be understood in the main.cpp file so they can compile correctly.

    If i include the .cpp file into my main.cpp file, it works, but I want to know the correct way of going about this.

    Thank you

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    How are you compiling your program? What compiler/IDE? You should be adding the .cpp file to your project, not including the file.

    Jim

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    13
    I'm not sure I understand. I created the file in vim, and I compiled it in the command prompt using g++ main.cpp

    I have a feeling you're asking a more in depth question than I can answer, I'm new to programming.

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    13
    How would I go about adding a .cpp file to my project? With a makefile?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I'm not sure I understand. I created the file in vim, and I compiled it in the command prompt using g++ main.cpp
    In which case, the simple answer is

    g++ main.cpp webcounter.cpp

    When you get a few more files, this gets cumbersome, at which point you'll be eager to know about makefiles.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Does #include indirectly include the source file too?
    By Lord Asriel in forum C Programming
    Replies: 10
    Last Post: 11-30-2011, 08:20 AM
  2. Replies: 1
    Last Post: 11-06-2011, 06:20 PM
  3. basic difference between #include<> and #include""
    By gunjansethi in forum C Programming
    Replies: 1
    Last Post: 03-26-2010, 12:53 AM
  4. #include <windows.h> and #include <wininet.h>
    By steve1_rm in forum C++ Programming
    Replies: 4
    Last Post: 03-30-2009, 11:14 AM
  5. Which came first? #include <stdio.h> or #include <stdlib.h> ?
    By Brian in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 01-14-2002, 10:58 PM