From: Andrew Falanga on
On Feb 24, 11:07 am, Keith Thompson <ks...(a)mib.org> wrote:
> Andrew Falanga <af300...(a)gmail.com> writes:
> > Couple of disclaimers.  I'm using VS2008 so the compiler errors come
> > from that.  Though I'm using the sockaddr_in structure in my code, and
> > for this example, this question is not about socket programming.
>
> > I have the following:
>
> > sockaddr_in foo;  // sockaddr_in is a struct for those not familiar
>
> And where is it declared?  I presume it's declared in some header that
> you've #included, but you didn't show us the #include directive.  In
> short, you haven't shown us your actual code, only an approximation of
> it.  In some cases, that can make it impossible to diagnose the
> problem.  (In this case, you're lucky; the problem happens to be
> a fairly obvious one.)

As you noted, this was an easy problem. I apologize for not typing
the include statements. I guess I should not have deleted the comment
I had after the declaration of the variable. Before posting, I had
this:

sockaddr_in foo; // sockaddr_in is a struct for those not familiar


They are included with:

#include <winsock2.h>
#include <ws2tcpip.h>



>
> > In this form, I get the following errors:
> > error C2143: syntax error : missing ';' before '.'
> > error C4430: missing type specifier - int assumed.
> > error C2371: 'foo' : redefinition; different basic types
>
> I presume your compiler reports the line number for each error
> message.  You probably got all three errors on the same line, the

Indeed I did.

> (intended) assignment statement.  It would have been helpful if you
> had told us that, either by showing the line numbers or by telling
> us that all three errors referred to the assignment statement.

I will remember that for next time.

>
> > What's interesting is that *BEFORE* I place that assignment of 5075 to
> > foo.sin_port and I hover over the object named foo, IntelliSense sees
> > it as an object of type sockaddr_in.  However, after I make that
> > assignment, IntelliSense says that foo is an object of type int.  If
> > the assignment is moved to inside the body of main( ), there's no
> > problem.  The compiler is happy.
>
> > I know I'm violating some language constraint but I can't remember
> > what and the FAQ for this group didn't have a question regarding
> > structs, nor did the C++ FAQ-Lite mention this.  Also, a very brief
> > few minutes searching through the current draft for the C++ standard
> > didn't mention this, or more precisely, I didn't enter the correct
> > search phrases.  Would someone here, more knowledgeable than I,
> > straighten me out?
>
> Keep in mind that this newsgroup deals with both C and C++, which
> are two different languages.  Telling us from the beginning which
> language you're using would have been helpful; for example, we
> wouldn't have bothered to suggest using "struct sockaddr_in" if
> we had known you're using C++.
>

Sorry, on this one. When posting here I don't usually make that
mistake. This is C++, and I did mention having searched, albeit
briefly, through the current draft for the C++ standard.

Andy
From: Keith Thompson on
Andrew Falanga <af300wsm(a)gmail.com> writes:
> On Feb 24, 11:07 am, Keith Thompson <ks...(a)mib.org> wrote:
>> Andrew Falanga <af300...(a)gmail.com> writes:
>> > Couple of disclaimers.  I'm using VS2008 so the compiler errors come
>> > from that.  Though I'm using the sockaddr_in structure in my code, and
>> > for this example, this question is not about socket programming.
>>
>> > I have the following:
>>
>> > sockaddr_in foo;  // sockaddr_in is a struct for those not familiar
>>
>> And where is it declared?  I presume it's declared in some header that
>> you've #included, but you didn't show us the #include directive.  In
>> short, you haven't shown us your actual code, only an approximation of
>> it.  In some cases, that can make it impossible to diagnose the
>> problem.  (In this case, you're lucky; the problem happens to be
>> a fairly obvious one.)
>
> As you noted, this was an easy problem. I apologize for not typing
> the include statements. I guess I should not have deleted the comment
> I had after the declaration of the variable. Before posting, I had
> this:
>
> sockaddr_in foo; // sockaddr_in is a struct for those not familiar

You shouldn't have had to type anything. When posting source code,
you should copy-and-paste the exact same code that you fed to the
compiler. Re-typing it will almost inevitably introduce errors that
can mask the real problem.

[...]

Incidentally, I said that statements are not allowed ouside function
bodies. I had forgotten than C++ has a different definition of the
term "statement" than C has. In C++, a declaration is a kind of
statement (and of course declarations are permitted outside function
bodies). In C, declarations and statements are disjoint. Personally,
I find the C uses of the terms clearer, but we have to deal with the
way each language defines them.

--
Keith Thompson (The_Other_Keith) kst-u(a)mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"