Variable already defined in .obj; What is going on here?

后端 未结 6 1455
遇见更好的自我
遇见更好的自我 2021-01-20 09:43

head.h


#pragma once

namespace foo
{
    int bar;

    int funct1();
}

head.cpp

#include \"head.h\"

int foo::funct1()
{
         


        
6条回答
  •  醉话见心
    2021-01-20 10:04

    How is it being defined multiple times?

    It is defined once in head.cpp and once in main.cpp. That is a total of two times. This violates the one definition rule, which states that there may only be one definition for every variable.

    int bar;
    

    This is a definition of a variable. You've included it into two translation units.

    A variable can be declared without definition in an extern declaration:

    extern int bar;
    

    Replace the definition with such declaration, and put the definition into exactly one translation unit.

提交回复
热议问题