简体   繁体   中英

Extern variables C++

I have a struct that I want to fill out in a separate source file from where I am running main. In the header file I included the extern but when I go to define the variable in a source file it doesn't work.

    //This is a header file
    struct example {
         int data1;
         float data2;
         bool example;
    }

    extern example tmp;

And then in a source file:

example *tmp = new example;

I'm getting the error " Expected unqualified-id ". Why is that?

  • A semicolon is missing after the declaration of the struct.
  • The type in the extern declaration and the definition differs: example vs example* .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM