Author Topic: Templates and ld error  (Read 8615 times)

davereid20

  • Guest
Templates and ld error
« on: January 19, 2006, 11:59:12 pm »
I'm working with a templated Stack class and a templated Queue class (that has to implement two Stacks for storing data instead of just a linked list, etc). I'm having trouble understanding the error message I get when I try to compile (source and project file is included in zip file).

Code
-------------- Build: default in pretest ---------------
mingw32-g++.exe -Wall  -I- -I. -I"C:\Program Files\MinGW\include" -I -c TestStack.cpp -o .objs\TestStack.o
mingw32-g++.exe -L"C:\Program Files\MinGW\lib"  -o TestStack.exe .objs\TestStack.o   
.objs\TestStack.o:locale-misc-inst.cc:(.text+0x270): multiple definition of `mainCRTStartup'
C:/Program Files/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../crt2.o:crt1.c:(.text+0x270): first defined here
.objs\TestStack.o:locale-misc-inst.cc:(.text+0x290): multiple definition of `WinMainCRTStartup'
C:/Program Files/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../crt2.o:crt1.c:(.text+0x290): first defined here
.objs\TestStack.o:locale-misc-inst.cc:(.text+0x2b0): multiple definition of `atexit'
C:/Program Files/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../crt2.o:crt1.c:(.text+0x2b0): first defined here
.objs\TestStack.o:locale-misc-inst.cc:(.text+0x2c0): multiple definition of `_onexit'
C:/Program Files/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../crt2.o:crt1.c:(.text+0x2c0): first defined here
.objs\TestStack.o:locale-misc-inst.cc:(.text+0x2d0): multiple definition of `__do_sjlj_init'
C:/Program Files/MinGW/bin/../lib/gcc/mingw32/3.4.5/crtbegin.o:crtstuff.c:(.text+0x0): first defined here
.objs\TestStack.o:crtstuff.c:(.bss+0x4): multiple definition of `_argc'
C:/Program Files/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../crt2.o:crt1.c:(.bss+0x4): first defined here
.objs\TestStack.o:crtstuff.c:(.bss+0x0): multiple definition of `_argv'
C:/Program Files/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../crt2.o:crt1.c:(.bss+0x0): first defined here
C:\Program Files\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\mingw32\bin\ld.exe: BFD 2.16.91 20050827 assertion fail ../../src/bfd/cofflink.c:1926
collect2: ld returned 5 exit status
Process terminated with status 1 (0 minutes, 3 seconds)
0 errors, 0 warnings

I logged into our computer science server to try and compile under UNIX, and I get this result:

Code
dareid:~ > g++ -Wall -c TestStack.cpp -o TestStack.o
TestStack.cpp: In function `int main()':
TestStack.cpp:10: error: parse error before `<<' token
MyQueue.h: In member function `value_type& MyQueue<value_type>::peek() const
   [with value_type = int]':
TestStack.cpp:10:   instantiated from here
MyQueue.h:187: error: no matching function for call to `MyQueue<int>::
   reverseStack(const MyStack<int>&, const MyStack<int>&) const'
MyQueue.h:57: error: candidates are: void
   MyQueue<value_type>::reverseStack(MyStack<value_type>&,
   MyStack<value_type>&) [with value_type = int]
TestStack.cpp:10:   instantiated from here
MyQueue.h:189: error: no matching function for call to `MyQueue<int>::
   reverseStack(const MyStack<int>&, const MyStack<int>&) const'
MyQueue.h:57: error: candidates are: void
   MyQueue<value_type>::reverseStack(MyStack<value_type>&,
   MyStack<value_type>&) [with value_type = int]
dareid:~ >

Can anyone help me figure out what's going on?

UPDATE: I compiled again, and I recieved the same result as compiled under UNIX (no longer getting the strange ld error) but I'm still not sure what's wrong.

[attachment deleted by admin]
« Last Edit: January 20, 2006, 12:05:49 am by davereid20 »

Offline Ceniza

  • Developer
  • Lives here!
  • *****
  • Posts: 1441
    • CenizaSOFT
Re: Templates and ld error
« Reply #1 on: January 20, 2006, 03:00:57 am »
First fix: remove the SEMICOLON from:

Code: cpp
cout << q.peek(); << endl;

so it'll look like this:

Code: cpp
cout << q.peek() << endl;

Now, the next fix: peek() is defined as a const member function and it cannot call non-const member functions for the same class. reverseStack() takes two non-const references and is a non-const member function.

The easiest way to solve that is to make peek() a non-const member function. Any other solution would be a mix of mutable members and/or const_cast<>.

Trying to understand those errors caused when using templates can be a whole adventure.

P.S.: There's some kind of "translator" for those errors. It has been named here in the forums but I always forget its name. From what I read it still needs some work to get it respectably working with g++ 3.4.x.

[edit]
Please, next time you have such a question that isn't directly related to Code::Blocks, post it in the "General / off-topic" board.

I'm sure Don Corelone won't like these questions in this board. It's for troubleshooting in Code::Blocks itself.

Thanks.
[/edit]
« Last Edit: January 20, 2006, 03:33:50 am by Ceniza »

davereid20

  • Guest
Re: Templates and ld error
« Reply #2 on: January 20, 2006, 04:04:03 am »
I apologize for posting this in the wrong location. I didn't know if the ld error was Code::Blocks related or not. The program compiles fine on UNIX on GCC now, but not in Code::Blocks with MinGW. I'll try and search more about this weird error.

davereid20

  • Guest
Re: Templates and ld error
« Reply #3 on: January 20, 2006, 04:17:57 am »
This also compiles correctly just using the following from a command line:
Code: dos
mingw32-g++ -Wall -c TestStack.cpp -o TestStack.o
mingw32-g++ -Wall -o TestStack.exe TestStack.o
but NOT in Code::Blocks, so this is definately an issue with C::B.  I have no idea why.  The only wierd thing I can see right now is that when C::B compiles, it called
Code: dos
mingw32-g++.exe -Wall  -I- -I. -I"C:\Program Files\MinGW\include" -I -c TestStack.cpp -o .objs\TestStack.o
and I have no idea why there are so many -I's in it.
« Last Edit: January 20, 2006, 04:27:00 am by davereid20 »

Offline Ceniza

  • Developer
  • Lives here!
  • *****
  • Posts: 1441
    • CenizaSOFT
Re: Templates and ld error
« Reply #4 on: January 20, 2006, 04:35:12 am »
That bunch of -I must be the side effect of something already discussed in the forums, that mandrav decided to "fix".

The most suspicious is the empty one followed by -c.

Offline Michael

  • Lives here!
  • ****
  • Posts: 1608
Re: Templates and ld error
« Reply #5 on: January 20, 2006, 10:09:04 am »
That bunch of -I must be the side effect of something already discussed in the forums, that mandrav decided to "fix".

I got the bunch of -I with my C::B compiled yesterday (rev1813).

In the two header files, I have found each time the inclusion of the same header files:

Quote
#include <cassert>
#include <cstdlib>

This should be avoided.

Michael

Offline Michael

  • Lives here!
  • ****
  • Posts: 1608
Re: Templates and ld error
« Reply #6 on: January 21, 2006, 07:24:14 pm »
I got the code building fine with C::B/MinGW :). It also seems to work correctly.

I have applied the solution proposed by Ceniza and made peek() non-const. As told above, I have also commented the included files below from the MyStack.h and MyQueue.h:

Quote
#include <cassert>
#include <cstdlib>

Those included files are not needed.

At the beginning, I got the same problems as davereid20. Then, I have tried to add to the linker option --allow-multiple-definition, make a clean and a build. It worked :). Then I have tried without --allow-multiple-definition and it worked too (before building, I have made a clean). It seems that just a clean was needed :).

Michael