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

后端 未结 6 1629
遇见更好的自我
遇见更好的自我 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:16

    The header head.h is included in two compilation units head.cpp and main.cpp. So the variable bar is defined twice. You could declare the variable without its definition the following way

    #pragma once
    
    namespace foo
    {
        extern int bar;
    
        int funct1();
    }
    

    and then define it in some cpp module.

提交回复
热议问题