User forums > Help
Templates and ld error
davereid20:
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
--- End code ---
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:~ >
--- End code ---
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]
Ceniza:
First fix: remove the SEMICOLON from:
--- Code: (cpp) ---cout << q.peek(); << endl;
--- End code ---
so it'll look like this:
--- Code: (cpp) ---cout << q.peek() << endl;
--- End code ---
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]
davereid20:
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:
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
--- End code ---
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
--- End code ---
and I have no idea why there are so many -I's in it.
Ceniza:
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.
Navigation
[0] Message Index
[#] Next page
Go to full version