Compilation Error

S

Sunil Varma

other.cpp
----------------
int arr[10] = {0};


main.cpp
----------------
#include <iostream>
using namespace std;

extern int arr[];

void main()
{
cout<<sizeof(arr)<<endl;
}


When I try to build the above code, I'm getting the following
compilation error.

f:\programs\nest\nest\main.cpp(23) : error C2070: 'int []': illegal
sizeof operand

I would like to know the reason why I get a compilation error.
When I change the extern to

extern int *arr;

the code compiles properly.

Thanks in anticipation.

Sunil
 
A

Alf P. Steinbach

* Sunil Varma:
other.cpp
----------------
int arr[10] = {0};
OK.


main.cpp
----------------
#include <iostream>
using namespace std;

extern int arr[];
OK.


void main()

The result type of 'main' must be 'int', both in C and C++.

No other result type is allowed.

If your compiler accepts 'void' then the compiler is non-conforming (one would
guess that you're using Microsoft's Visual C++).

{
cout<<sizeof(arr)<<endl;
}


When I try to build the above code, I'm getting the following
compilation error.

f:\programs\nest\nest\main.cpp(23) : error C2070: 'int []': illegal
sizeof operand

Yes, the size is not known locally in 'main'.

I would like to know the reason why I get a compilation error.
When I change the extern to

extern int *arr;

the code compiles properly.

But only gives you the size of a pointer, not of the array.

For what you appear to want a reasonable solution is to use 'std::vector', and
to avoid running into the static initialization order fiasco (see the FAQ)
provide it via e.g. a Meyers' singleton, like

<code file="other.cpp">
#include <vector>

std::vector<int>& arr()
{
static std::vector<int> theArray( 10 );
return theArray;
}
</code>

<code file="main.cpp">
#include <vector>
#include <iostream>

std::vector<int>& arr();

int main()
{
using namespace std;
cout << arr().size() << endl;
}
</code>

*However*, global variables, which this essentially is, are generally Very
Evil(TM) and should be avoided.

It's also a good idea to provide a header for a separately compiled file or set
of files like that.

What book are you using that doesn't explain use of 'std::vector' and doesn't
explain use of headers and doesn't explain the evilness of global variables?


Cheers & hth.,

- Alf
 
S

Sunil Varma

* Sunil Varma:
other.cpp
----------------
int arr[10] = {0};
OK.

main.cpp
extern int arr[];
OK.

void main()

The result type of 'main' must be 'int', both in C and C++.

No other result type is allowed.

If your compiler accepts 'void' then the compiler is non-conforming (one would
guess that you're using Microsoft's Visual C++).
{
   cout<<sizeof(arr)<<endl;
}
When I try to build the above code, I'm getting the following
compilation error.
f:\programs\nest\nest\main.cpp(23) : error C2070: 'int []': illegal
sizeof operand

Yes, the size is not known locally in 'main'.
I would like to know the reason why I get a compilation error.
When I change the extern to
extern int *arr;
the code compiles properly.

But only gives you the size of a pointer, not of the array.

For what you appear to want a reasonable solution is to use 'std::vector', and
to avoid running into the static initialization order fiasco (see the FAQ)
provide it via e.g. a Meyers' singleton, like

   <code file="other.cpp">
   #include <vector>

   std::vector<int>& arr()
   {
       static std::vector<int> theArray( 10 );
       return theArray;
   }
   </code>

   <code file="main.cpp">
   #include <vector>
   #include <iostream>

   std::vector<int>& arr();

   int main()
   {
       using namespace std;
       cout << arr().size() << endl;
   }
   </code>

*However*, global variables, which this essentially is, are generally Very
Evil(TM) and should be avoided.

It's also a good idea to provide a header for a separately compiled file or set
of files like that.

What book are you using that doesn't explain use of 'std::vector' and doesn't
explain use of headers and doesn't explain the evilness of global variables?

Cheers & hth.,

- Alf

Thank you.
I've used vector and it's working fine.

Sunil
 
J

James Kanze

other.cpp
extern int arr[];

void main()

BTW: main must return int, not void.
{
cout<<sizeof(arr)<<endl;
}
When I try to build the above code, I'm getting the following
compilation error.
f:\programs\nest\nest\main.cpp(23) : error C2070: 'int []': illegal
sizeof operand
I would like to know the reason why I get a compilation error.

Because the code is illegal. In C++ terms "A class that has
been declared but not defined, or an array of unknown size or
incomplete element type is an incompletely-defined object type.
Incompletely-defined objec types and void types are incomplete
types." And sizeof requires a complete type.

From a practical point of view: sizeof is an integral constant
expression---it evaluates to a compile time constant,
corresponding to the number of bytes occupied by the object.
And given "extern int array[];" (and separate compilation), how
is the compiler supposed to know how many bytes are in the
object.
When I change the extern to
extern int *arr;
the code compiles properly.

For some definition of "properly", I suppose. If you define
arr to be an array, but declare it as a pointer in a different
translation unit, you have undefined behavior, and in practice,
your code won't work. At all. (A good system will give you an
error at link time, but good systems, at least in this respect,
are hard to come by.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top