Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Fatal error LNK1169: one or more multiply defined symbols found

Fatal error LNK1169: one or more multiply defined symbols found

Scheduled Pinned Locked Moved C++ Gurus
3 Posts 3 Posters 13.1k Views
  • 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on 19 May 2014, 13:12 last edited by
    #1

    Hello everybody;
    I have a problem with my class when i call my class in more than once...
    How can I solve it?

    My error is :

    @
    1>------ Rebuild All started: Project: Project7, Configuration: Debug Win32 ------
    1> stdafx.cpp
    1> main.cpp
    1> m2.cpp
    1> m1.cpp
    1>main.obj : error LNK2005: "class MyClass * Action" (?Action@@3PAVMyClass@@A) already defined in m2.obj
    1>c:\users\Domain\documents\visual studio 2013\Projects\Project7\Debug\Project7.exe : fatal error LNK1169: one or more multiply defined symbols found
    ========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
    @

    My H1.h file :
    @
    #ifndef M1_H
    #define M1_H

    #include <iostream>

    class MyClass
    {
    public:
    MyClass();
    ~MyClass();

    private:

    public:
    std::string P();

    };

    #endif

    @

    My H1.cpp file :

    @
    #include "stdafx.h"
    #include "m1.h"

    MyClass::MyClass() {}
    MyClass::~MyClass() {}

    std::string MyClass::P()

    {

    return "Test";
    }

    @

    My H2.h file :

    @
    #ifndef M2_H
    #define M2_H

    #include <iostream>

    class MyClass2
    {
    public:
    MyClass2();
    ~MyClass2();

    private:

    public:
    std::string P2();

    };

    #endif

    @

    My h2.cpp file :

    @

    #include "stdafx.h"
    #include "m1.h"
    #include "m2.h"

    MyClass2::MyClass2() {}
    MyClass2::~MyClass2() {}

    MyClass * Action = new MyClass;

    std::string MyClass2::P2()

    {

    return "Test";
    }

    @

    and my main

    @
    #include "stdafx.h"
    #include <iostream>
    #include "m1.h"

    using namespace std;

    MyClass * Action = new MyClass;

    int main()

    {

    getchar();

    };
    @

    Who can help me for solve this problem , what happened after compile please explain this error to me :)

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on 19 May 2014, 17:36 last edited by
      #2

      Hi,

      the problem is that you define two "global" objects with the same name

      Action in main.cpp

      Action in h2.cpp

      in C++ "global" objects lives in the same namespace so you cannot use the same name.

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      0
      • L Offline
        L Offline
        lutzhell
        wrote on 27 May 2014, 06:32 last edited by
        #3

        Hi,

        the more general question is: why do you need these "global" objects anyway especially in main? Since Action is a pointer it can be passed to any function without any relevant costs.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved