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

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

    This foo namespace-level bar declaration:

    namespace foo
    {
        int bar;
    }
    

    is actually a definition.

    To make it a declaration, mark the bar as extern in head.h:

    namespace foo
    {
        extern int bar;
    }
    

    Then define it in head.cpp:

    int foo::bar = 0;
    

提交回复
热议问题