Archived

This topic is now archived and is closed to further replies.

Query about gcc

This topic is 5617 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, Hope this isn't a stupid question. Usually I compile c++ code using the Borland compiler under windows, however recently I've been trying to write some cross platform stuff, compiling under Borland and gcc. Everything was going fine until I took some old code that had compiled fine under Borland. It was a set of partial ports of Java classes to c++. I have three classes: Object, Class and String. Within my Class definition I have the following method:
Class Class::forName(String className){
   return Class(className);
}
  
Upon compiling this with gcc (2.96) I get the following error:
No matching function for call to Class Class (Class).
It appears to be mistaking the type of className, thinking it's of type Class instead of Type String. Interestingly An explicit cast of className to String (return Class((String)className)) does not change anything but a cast to Object changes the error to:
No matching function for call to Class Class (Object)
which is what I would expect. There are a lot of other compile errors at the moment, and it's quite possible that something else is causing the problem, but what really has me stumped is that changing the code to:
Class Class::forName(String className){
   return *(new Class(className));
}
  
solves the problem. I thought that Class() and *(new Class()) should have identical behaviour? Thanks in advance for you help, Enigma edit: html tags [edited by - Enigma on July 23, 2002 1:05:09 PM]

Share this post


Link to post
Share on other sites
yep. wierd.

usually the no matching function error means you don''t have that function you are writing in your header with exactly the same syntax.

the *(new class()) thinger is ultra wierd. i wouldn''t have expected that to solve the problem. anyway, make sure your header file has the correct def of the fctn. beyond that, break out the voodoo dolls and start chanting.

-me

Share this post


Link to post
Share on other sites
The full error message (for this error) is:

In file included from java clones/class.h:37,
from java clones/string.cpp:27,
from java clones/string.h:411,
from java clones/class.h:14,
from java clones/object.cpp:15,
from java clones/object.h:71,
from java clones/string.h:24,
from PSKTimerMark.h:10,
from PSKSystemTimer.h:16,
from temp.cpp:1:
java clones/class.cpp: In function `Class Class::forName (String)'':
java clones/class.cpp:21: no matching function for call to
`Class::Class (Class)''
java clones/class.cpp:17: candidates are: Class::Class (String)
java clones/class.h:33: Class::Class (Class &)


I''m still working my way through the other errors in the hope that I''ll stumble across a reason - no luck yet.

Enigma

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I don''t know what the problem is, but GCC 2.96 is notoriously broken (it was a developer version that never made it to the release because it was broken, but RedHat shipped it for some odd reason). Try a 2.95 or 3.1 and see if that works.

Share this post


Link to post
Share on other sites
I''ve tried 3.1 and the problem still occurs.

Oh well - I always knew I''d have to re-write that code at somepoint. I suppose now is as good a time as any!

Thanks for the suggestions,

Enigma

Share this post


Link to post
Share on other sites
I tried putting in a copy constructor but it didn''t change anything (I hope I didn''t just mess up the constuctor - I don''t usually use copy constructors). I''ll try adding them to a couple of other classes and see if that makes any difference. Thanks for the suggestion.

Prefect: I''d never actually thought about that! The *(new instance()) was something I tried when I was still very new to c/c++. I don''t use it now because it''s redundant but it''s one of the things I sometimes try if I can''t figure an error out. Thanks for pointing out what should have been blatantly obvious!

Enigma

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You don''t actually need a copy ctor, it would have been
generated by the compiler anyway.
Do you include the right header file, however ? If not,
the compiler will not know what you''re talking about,
and not know if it has to generate a copy ctor, or use
yours if you have one (as it doesn''t know).

Share this post


Link to post
Share on other sites
That''s what I would have expected, expecially as the error message mentions Class::Class(&Class) as a candidate. The Borland compiler certainly adds copy constructors for you if you don''t define them yourself.

However, having said all of that it appears that C-Junkie did hit the nail on the head - Having added a copy constructor and changed it and all existing copy constructors to class::class(const class& name) instead of class::class(class& name) the program compiles fine (It doesn''t link yet, but it compiles!).

Does anyone know why the parameter to the copy constructor needs to be const? I had a look around the web but every tutorial just uses const, none of them explain why. Under Borland I''ve never had to use const.

Cheers,

Enigma

Share this post


Link to post
Share on other sites
Another problem

I've gotten rid of all the link errors that I understand, but I'm left with the following list of errors:

temp: In function `__divdi3':
/usr/src/build/87732-i386/BUILD/gcc-2.96-20000731/obj-i386-redhat-linux/gcc/../../gcc/libgcc2.c(.rodata+0x0): multiple definition of `_fp_hw'
/usr/lib/crt1.o(.rodata+0x0): first defined here
temp: In function `_init':
temp(.init+0x0): multiple definition of `_init'
/usr/lib/crti.o(.init+0x0): first defined here
temp: In function `_start':
temp(.text+0x0): multiple definition of `_start'
/usr/lib/crt1.o(.text+0x0): first defined here
temp: In function `__divdi3':
/usr/src/build/87732-i386/BUILD/gcc-2.96-20000731/obj-i386-redhat-linux/gcc/../../gcc/libgcc2.c(.fini+0x0): multiple definition of `_fini'
/usr/lib/crti.o(.fini+0x0): first defined here
temp: In function `__divdi3':
/usr/src/build/87732-i386/BUILD/gcc-2.96-20000731/obj-i386-redhat-linux/gcc/../../gcc/libgcc2.c(*ABS*+0x804a708): multiple definition of `_GLOBAL_OFFSET_TABLE_'
/usr/lib/crt1.o(.got.plt+0x0): first defined here
temp: In function `__divdi3':
/usr/src/build/87732-i386/BUILD/gcc-2.96-20000731/obj-i386-redhat-linux/gcc/../../gcc/libgcc2.c(.rodata+0x4): multiple definition of `_IO_stdin_used'
/usr/lib/crt1.o(.rodata+0x4): first defined here
temp: In function `__divdi3':
/usr/src/build/87732-i386/BUILD/gcc-2.96-20000731/obj-i386-redhat-linux/gcc/../../gcc/libgcc2.c(.data+0x0): multiple definition of `__data_start'
/usr/lib/crt1.o(.data+0x0): first defined here
/usr/lib/crt1.o(.dynamic+0x0): multiple definition of `_DYNAMIC'
collect2: ld returned 1 exit status

It looks to me like the headers I'm including are conflicting or messed up and redefining things. Does anyone have any idea what's going on here and whether it's likely to be because of my code, because of any odd headers I may be including, or because the headers are messed up?

Cheers,

Enigma

[edited by - Enigma on July 26, 2002 11:12:57 AM]

Share this post


Link to post
Share on other sites
looks to me like your object files already have libraries linked with them...

Can we see you Makefile (or relevant portions of it)

or are you doing this by hand? (or some ide?)

Share this post


Link to post
Share on other sites
This is all done by hand!

The command line I use is:

g++ -o <filename> <list of cpp files>

Unfortunately I can't remember all the files at the moment - If it'll help I can post them tommorrow when I have access to the unix machine they're currently stored on. All the files I compile are my own c++ files and there are no libraries. I suspect that something messed up when I installed gcc 3.1, but I don't know enough about gcc to be sure. Interestingly I got an error when running a very simple program (literally just print a line to cout). It was something to do with a shared library. I'll post details tommorrow when I can reproduce it.

Thanks for your continuing help,

Enigma

Edit: tags!

[edited by - Enigma on July 28, 2002 6:07:56 PM]

Share this post


Link to post
Share on other sites
OK, I''ve compiled it on a different machine and it linked fine (segfaults during execution, but it linked OK) - looks like I''m going to have to reinstall gcc on the old machine.

Enigma

Share this post


Link to post
Share on other sites
Yay!!

It works!!

For some unknown reason the code compiled and linked fine on my old machine this morning. I still have a problem with gcc 3.1, any program compiled with it gives the following error:

error while loading shared libraries: libstdc++.so.4:
cannot open shared object file: No such file or directory

I''ll have a play around and see if I can fix it.

Cheers for all your help guys and be warned - I have a lot of coding left to do - I may well be back!!!

Cheers

Enigma

Share this post


Link to post
Share on other sites