0

I saw this question and I tried to do as the answer to that question said. To use the extern keyword in the header file to define an array and then declare it outside of that namespace or class in a other cpp file.

It didn't work for me really, I'm not sure if it because I'm using a void pointer array (i.e void* array[]) or if it's just my ignorance that prevents me from seeing the problem.

This is the shortest example I can come up with:
[cpp.cpp]

#include "h.h"

void main(){
    void* a::b[] = {
        a::c = a::d(1)
    };
}

[h.h]

namespace a{
    struct T* c;
    struct T* d(int e);
    extern void* b[];
}

So the problem is that I receive the error: IntelliSense: variable "a::b" cannot be defined in the current scope

And I have no clue why that is.

12
  • 3
    How is it briefer if you have to also post that disclaimer? Commented Aug 16, 2014 at 18:15
  • 2
    int is one character shorter than void... a::b is declared outside of a function, the definition should be outside as well, why are you writing it inside main? Commented Aug 16, 2014 at 18:20
  • So instead of fixing void main() you deleted the reasoning for why you typed something incorrect to begin with. Brilliant. And the definition of a::b needs to be at namespace scope, not block scope. Commented Aug 16, 2014 at 18:21
  • @MarcGlisse Because I wanted the shortest version possible, I used void main, because then I don't have to return anything. Commented Aug 16, 2014 at 18:22
  • @Linus It's perfectly legal to omit the return statement in int main() { ... } Commented Aug 16, 2014 at 18:23

1 Answer 1

2

First, you should declare main() as int ! See here why.

Declaring your array as extern in a namespace means that it belongs to the namespace but is defined somewhere ele, normally in a separate compilation unit.

Unfortunately, in your main(), you try to redefine the element as a local variable. This explains the error message you receive.

You shoud do as follows:

#include "h.h"
void* a::b[] { a::c, a::d(1) };         // global variable belonging to namespace

int main()                  // int!!!
{
    /* your code here */  
}

The code will compile. The fact that a::b[] is defined in the same compiling unit is accepted. But the linker will complain because a::d(1) is a call to the function d returning a pointer to a struct, and this function is defined nowhere.

Therfore you should also define this:

namespace a { 
    struct T* d(int e)
    {
        return nullptr;         // in reality you should return a pointer to struct T
    }
} 

Interestingly, struct T does not need to work for this code to compile and link.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.