[lug] Re: Reagarding structures
Yes, namespaces and scopes.
I'll try to be short:
First: scopes. Every variable, type, function, etc. has its own scope,
which usually begins at the point of declaration, and last until the
end of the block. Eg.:
int main()
{
i=1; //error
if (true) {
int i; //scope begins
i=5; //ok
} //scope of i ends
i=5; //error
}
Namespaces are some kind of "declaration of a scope". If you define a
namespace, end you want to access its contents you must tell this to
the compiler:
namespace a {
int f() {}
}
int main()
{
f(); //error, no f declared
a::f(); //ok
}
Ok then, but why can't use the struct from inside main()?
Because in C++ every function is implicitly a namespace, in your
function definition the compiler "doesn't know" what that struct is.
In that scope no structs are defined.
And another thing: scopes, types, namespaces are present at
compile-time. They are not "passed" through a function call.
Now that was short....
I can be shorter though: It's by design :)
Hi!
On 1/3/06, sunil vvn <sunil.vvn@gmail.com> wrote:
>
> Adam,
>
> It is working absolutely fine when I decalred and defined
> outside the main.
> I am having certain doubts about this program..........if it is
> possible could U clarify me?
> I declared and defined the struct varaible in inside the main,
> If I passing from the main why I am unable to access it ?is
> there any specific reason
>
> Rgrds
> Sunil
> On 1/3/06, Adam Visegradi <a.visegradi@gmail.com> wrote:
> > Probably because struct test is inside main().
> >
> > "from 'struct main::test [5]' to 'struct test []'" //notice the `main::'
> > and
> > "error C2027: use of undefined type 'test'"
> >
> > Try this:
> > void func(struct main::test x[]);
> > or define struct test {} globally.
> >
> > It's just a guess, but I hope it helps.
> >
> > Nice day! :)
> >
> > On 1/3/06, sunil vvn <sunil.vvn@gmail.com> wrote:
> > >
> > > Hi All,
> > >
> > > Could U anybody suggest me why this is throwing error,plz
> > > rectify me where I am going wrong.
> > >
> > > #include<stdio.h>
> > > void func(struct test x[]);
> > > int main()
> > > {
> > >
> > > struct test
> > > {
> > > int y;
> > > };
> > > struct test x[] = {20,30,40,50,60};
> > > func(x);
> > > return 0;
> > > }
> > >
> > > void func(struct test x[])
> > > {
> > > int mid = 2;
> > >
> > > printf("%d\n",x[mid].y );
> > >
> > > }
> > >
> > > I got the following errors when I execute in the vc++ editor
> > >
> > > --------------------Configuration: test - Win32 Debug--------------------
> > > Compiling...
> > > test.cpp
> > > E:\sunil\fiscus\test\test.cpp(11) : error C2664: 'func' : cannot
> > > convert parameter 1 from 'struct main::test [5]' to 'struct test []'
> > > Types pointed to are unrelated; conversion requires
> > > reinterpret_cast, C-style cast or function-style cast
> > > E:\sunil\fiscus\test\test.cpp(19) : error C2036: 'struct test []' :
> > unknown size
> > > E:\sunil\fiscus\test\test.cpp(19) : error C2027: use of undefined type
> > 'test'
> > > E:\sunil\fiscus\test\test.cpp(2) : see declaration of 'test'
> > > E:\sunil\fiscus\test\test.cpp(19) : error C2228: left of '.y' must
> > > have class/struct/union type
> > > Error executing cl.exe.
> > >
> > > test.obj - 4 error(s), 0 warning(s)
> > >
> > > -----------------------------------
> > >
> > > Regrads
> > > Sunil
> > >
> >
> >
> >
> > --
> > Visegradi Adam
> >
>
--
Visegradi Adam
0 Comments:
Yorum Gönder
<< Home