public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Ross Boylan <RossBoylan@stanfordalumni.org>
To: Bill Priest <priestwilliaml@yahoo.com>
Cc: gcc-help@gcc.gnu.org, Ross Boylan <rboylan@post.harvard.edu>
Subject: Re: Should this compile and link??
Date: Wed, 16 Jan 2002 14:28:00 -0000	[thread overview]
Message-ID: <20020116222626.GL1398@wheat.boylan.org> (raw)
In-Reply-To: <20020116142729.2e26d70d.priestwilliaml@yahoo.com>

In your example, you have created an anonymous structure, and then
created a typedef for it.

Since it is anonymous, there is no way to refer to it, and thus your
definition of f1 doesn't get associated properly. I'm a little
suprised the compiler doesn't give you an error in c4e3_func.cc when
you refer to the class or structure TEST_TYPE.

Consider this to clarify things:

struct SILLY {
	void f1(void);
};
typedef SILLY TEST_TYPE;

If you want to define f1, you must say
SILLY::f1
not TEST_TYPE::f1.

However, in your example there is no name SILLY, so no way to refer to
the function.

Alternately, you could 
typedef struct {
	void f1(void) (puts("something");};
} TEST_TYPE;

and then you'd probably be OK.

I haven't communed with the standards statement, but I think this is
what's going on.


On Wed, Jan 16, 2002 at 02:27:29PM -0600, Bill Priest wrote:
> c4e3.cc
> ---------------
> #include "c4e3.h"
> 
> int main(void)
> {
> 	TEST_TYPE test1;
> 
> 	test1.f1();
> }
> ----------------
> c4e3_func.cc
> ----------------
> #include <cstdio>
> #include "c4e3.h"
> 
> void TEST_TYPE::f1(void)
> {
> 	puts("c4e3");
> }
> ------------------
> 
> c4e3.h
> ------------------
> #ifndef C4E3_H
> #define C4E3_H
> 
> typedef struct // TEST_TYPE
> {
> 	void f1(void);
> } TEST_TYPE;
> 
> #endif
> --------------------
> 
> w/ stock g++ 3.0.3 the following commands
> 
> g++    -c -o c4e3.o c4e3.cc
> g++    -c -o c4e3_func.o c4e3_func.cc
> g++ c4e3.o c4e3_func.o -o c4e3
> c4e3.o: In function `main':
> c4e3.o(.text+0x11): undefined reference to `TEST_TYPE::f1()'
> 
> If I uncomment TEST_TYPE in c4e3 everything compiles and links
> as it should.
> 
> If I change c4e3.cc to include "c4e3_func.cc" and change the command to
> g++ -o c4e3 c4e3.cc
> then it compiles and links even if the c4e3.h file is unchanged (ie TEST_TYPE is commented out).
> 
> This doesn't make a whole lot of sense to me; but I'm not a C++ expert or even novice.  
> Neither of my books (Thinking in C++ and C++ for C programmers) seems to cover this explicitly.
> 
> Is there something I'm missing here.  Scoping or namespace resolution was the only thing I could
> come up with but didn't see anything in either of the books.
> 
> Please set me straight,
> 
> Bill
> 
> _________________________________________________________
> Do You Yahoo!?
> Get your free @yahoo.com address at http://mail.yahoo.com
> 

      reply	other threads:[~2002-01-16 22:28 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-01-16 12:34 Bill Priest
2002-01-16 14:28 ` Ross Boylan [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20020116222626.GL1398@wheat.boylan.org \
    --to=rossboylan@stanfordalumni.org \
    --cc=gcc-help@gcc.gnu.org \
    --cc=priestwilliaml@yahoo.com \
    --cc=rboylan@post.harvard.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).