Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Declare in form1.h causes error

Declare in form1.h causes error

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpwinformsdebuggingquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jantimmerman
    wrote on last edited by
    #1

    Hello, I was experiencing a strange error that I tracked down to be a declaration in Form1.h I attemped to reproduce the problem in a brand new project and to my surprise: the error appeared again. In the new "windows forms" project test1.cpp: #include "stdafx.h" #include "form1.h" Added to form1.h int test; error: test.obj : error LNK2005: "int test" (?test@@$$Q3HA) already defined in IncludeTest.obj D:\VC\IncludeTest\Debug\IncludeTest.exe : fatal error LNK1169: one or more multiply defined symbols found. I have tried int test in stdafx.h. This does compile though stdafx.h is also included in both the main and test.cpp. Project available here (44k, created with VC++ 2005) I thought this was according to c++ language rules, but apparently I am doing something wrong? Best regards, Jan Timmerman

    S R 2 Replies Last reply
    0
    • J jantimmerman

      Hello, I was experiencing a strange error that I tracked down to be a declaration in Form1.h I attemped to reproduce the problem in a brand new project and to my surprise: the error appeared again. In the new "windows forms" project test1.cpp: #include "stdafx.h" #include "form1.h" Added to form1.h int test; error: test.obj : error LNK2005: "int test" (?test@@$$Q3HA) already defined in IncludeTest.obj D:\VC\IncludeTest\Debug\IncludeTest.exe : fatal error LNK1169: one or more multiply defined symbols found. I have tried int test in stdafx.h. This does compile though stdafx.h is also included in both the main and test.cpp. Project available here (44k, created with VC++ 2005) I thought this was according to c++ language rules, but apparently I am doing something wrong? Best regards, Jan Timmerman

      S Offline
      S Offline
      Sebastian Schneider
      wrote on last edited by
      #2

      If you have not already, try adding guards to your header-file. I am not sure about your background, but each header-file should have a guard: test1.h: #ifndef _TEST1_H_ #define _TEST1_H_ ..... your header information ..... #endif test2.h: #ifndef _TEST2_H_ #define _TEST2_H_ ..... your header information ..... #endif etc.

      1 Reply Last reply
      0
      • J jantimmerman

        Hello, I was experiencing a strange error that I tracked down to be a declaration in Form1.h I attemped to reproduce the problem in a brand new project and to my surprise: the error appeared again. In the new "windows forms" project test1.cpp: #include "stdafx.h" #include "form1.h" Added to form1.h int test; error: test.obj : error LNK2005: "int test" (?test@@$$Q3HA) already defined in IncludeTest.obj D:\VC\IncludeTest\Debug\IncludeTest.exe : fatal error LNK1169: one or more multiply defined symbols found. I have tried int test in stdafx.h. This does compile though stdafx.h is also included in both the main and test.cpp. Project available here (44k, created with VC++ 2005) I thought this was according to c++ language rules, but apparently I am doing something wrong? Best regards, Jan Timmerman

        R Offline
        R Offline
        Ryan Binns
        wrote on last edited by
        #3

        The problem is that memory for that variable is being allocated in every source file that includes that header file - the linker is unable to determine which one to keep and which ones to throw away. All it knows is that the two memory locations have the same name - it doesn't know that they are the same variable. The solution is to mark the variable as extern in the header file, and declare it as normal in exactly one of the source files.

        Ryan

        "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

        J 1 Reply Last reply
        0
        • R Ryan Binns

          The problem is that memory for that variable is being allocated in every source file that includes that header file - the linker is unable to determine which one to keep and which ones to throw away. All it knows is that the two memory locations have the same name - it doesn't know that they are the same variable. The solution is to mark the variable as extern in the header file, and declare it as normal in exactly one of the source files.

          Ryan

          "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

          J Offline
          J Offline
          jantimmerman
          wrote on last edited by
          #4

          Perhaps that might work. This does not explain though why it is allowed to say int test; in stdafx.h (which is included also in a lot of places) Also I thought int test; is a declaration here. It would be illegal to write int test=3;. I will give your suggestions a closer look, thanks for your quick replies! -- modified at 7:47 Monday 6th March, 2006 Well, it works, thanks a lot!

          R 1 Reply Last reply
          0
          • J jantimmerman

            Perhaps that might work. This does not explain though why it is allowed to say int test; in stdafx.h (which is included also in a lot of places) Also I thought int test; is a declaration here. It would be illegal to write int test=3;. I will give your suggestions a closer look, thanks for your quick replies! -- modified at 7:47 Monday 6th March, 2006 Well, it works, thanks a lot!

            R Offline
            R Offline
            Ryan Binns
            wrote on last edited by
            #5

            jantimmerman wrote:

            This does not explain though why it is allowed to say int test; in stdafx.h (which is included also in a lot of places)

            It's because you're using precompiled headers. VC++ only compiles the header once, and just references it in all the other files that include it. If you turn off precompiled headers, you'll have the same problem there.

            Ryan

            "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups