Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7071053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:38:27+00:00 2026-05-28T05:38:27+00:00

quick question. I am trying to get C++ nailed down, and today I spent

  • 0

quick question.

I am trying to get C++ nailed down, and today I spent hours with a double definition linker error(“this has already been defined!”) and I finally realised it’s because I had the layout as such:

  • main.cpp

    #include Dog.cpp
    
  • Dog.cpp

    #include Dog.h
    
  • Dog.h

    // (Dog class and prototype of test function)
    

And now that I’ve cleared that up by including the Dog.h instead of the Dog.cpp in the main.cpp.

By including the .h file, does the .cpp file with the identical prefix get compiled with the program?

I was astounded when the program ran with only the .h included and no references whatsoever to Dog.cpp. I spent ages Googling but no answers really helped me understand what was going on.

Edit: I forgot to add that I prototyped in the .h, and defined the function for the class in the .cpp and that’s what gave me the “already defined” error.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:38:28+00:00Added an answer on May 28, 2026 at 5:38 am

    By including the .h file, does the .cpp file with the identical prefix get compiled with the program? I was astounded when the program ran with only the .h included and no references whatsoever to Dog.cpp.

    No.

    Your program is built in phases.

    • For the compilation phase, only declarations are needed in each translation unit (roughly equivalent to a single .cpp file with #includes resolved). The reason that declarations even exist in the first place is as a kind of “promise” that the full function definition will be found later.

      g++ -c Dog.cpp               # produces `Dog.o`
      g++ -c main.cpp              # produces `main.o`
      
    • For the linking phase, symbols are resolved between translation units. You must be linking together the result of compiling Dog.cpp and of compiling main.cpp (perhaps your IDE is doing this for you?), and this link process finds all the correct function definitions between them to produce the final executable.

      g++ Dog.o main.o -o program  # produces executable `program`
      

      (Either that, or you actually haven’t got to the link phase yet, and merely have an object file (Dog.o); you can’t execute it, partially because it doesn’t have all the function definitions in.)

    The two phases can be done at the same time, with the “shorthand”:

    g++ Dog.cpp main.cpp -o program  # compiles, links and produces executable
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's a quick question I've been banging my head against today. I'm trying to
I had a quick question. I am trying to get an user-authentication form to
Hi guys just a quick question, After having a nightmare trying to get an
Quick question, what is wrong with this? (get) :: [a] -> Int -> a
Just getting started with Powershell and have a quick question. Trying to run this
Quick question, I'm trying to pass a value to a variable and then run
Here's what I hope is a quick question... I am trying to set a
I am new to Ruby and had a quick question. I am trying to
Just a quick question. I am trying to help a friend with his website.
Quick question I am trying to move an uploaded file to a directory outside

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.