Fatal error LNK1169: one or more multiply defined symbols found



  • 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 :)



  • 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.



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.