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 145893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:30:16+00:00 2026-05-11T08:30:16+00:00

I seem to be snagging on a fundamental but I can’t find the solution

  • 0

I seem to be snagging on a fundamental but I can’t find the solution anywhere. Anywho, will go ahead and explain.

I have a program consisting of three files; main.ccp, add.h, add.cpp.

I declare the class ‘SA’ in add.h and have all my functions defined in add.cpp.

additional.h

class SA {     ... public     int x; } Obj1, Obj2; 

main.ccp

#include "additional.h"   int main() {      Obj1.x = 5;      ... } 

This gives me a link error on compiling: error LNK2005: "class SA Obj1" (?Obj1@@3VSA@@A) already defined in main.obj

The only deffinition of the object occurs in add.h, and nowhere else. The program compiles just fine if I declare the objects in the main and not the header:

main.ccp

#include "additional.h"   int main() {      SA Obj1;     Obj1.x = 5;      ... } 

The issue is that I want to use the objects primarily within add.cpp, but still need to initialise several public values through main.cpp. Any words of wisdom?

  • 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. 2026-05-11T08:30:17+00:00Added an answer on May 11, 2026 at 8:30 am

    Define Obj1 and Obj2 in your .cpp instead of at .h

    add.h

    class SA {  ... public     int x; }; 

    main.cpp

    #include "additional.h"   SA Obj1, Obj2;  int main() {   Obj1.x = 5;   ... } 

    If you want to declare Obj1 and Obj2 in your .h file, add extern in the .h file like so:

    extern SA Obj1, Obj2; 

    but you should declare the objects in a .cpp file in your project:

    main.cpp

    SA Obj1, Obj2; 

    The reason for this is that everytime you include the .h file, you are declaring Obj1 and Obj2. So if you include the .h file two times, you will create two instance of Obj1 and Obj2. By adding the keyword extern, you are telling the compiler that you have already decalred the two variables somewhere in your project (preferably, in a .cpp file).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I seem to have some files in my Xcode project that I can't erase.
Seem's to be a stupid problem but here I go... I have a table
Seem to be getting an error but can't figure out how to drill down
I seem to have a problem, I have a growing business to run but
I seem to have a problem with getting MVC to fill in my custom
I seem to remember that Regular Expressions in DotNet have a special mechanism that
I seem to have run into a bit of trouble this morning with removeClass
Seem to have a problem starting my Java app: Exception in thread main java.lang.NoClassDefFoundError:
I seem to have a memory leak and I'm not sure how to fix.
I seem to have hit on a scenario where when I run mstest on

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.