* template under gcc version 3.3.2
@ 2005-01-29 13:42 Morten.Gulbrandsen
2005-01-29 20:59 ` corey taylor
0 siblings, 1 reply; 5+ messages in thread
From: Morten.Gulbrandsen @ 2005-01-29 13:42 UTC (permalink / raw)
To: gcc-help
hello,
I'm trying to compile this piece of code from here,
http://www.josuttis.com/cppbook/tmpl/stest5.cpp.html
instead of stack5.hpp I inserted
stack5decl.hpp <http://www.josuttis.com/cppbook/tmpl/stack5decl.hpp.html>
stack5assign.hpp
<http://www.josuttis.com/cppbook/tmpl/stack5assign.hpp.html>
Please help.
I'm using gcc under solaris,
bash-2.05$ g++ -v
Reading specs from /opt/sfw/gcc-3/lib/gcc-lib/i386-pc-solaris2.9/3.3.2/specs
Configured with: ../gcc-3.3.2/configure --prefix=/opt/sfw/gcc-3
--with-ld=/usr/ccs/bin/ld --with-as=/usr/ccs/bin/as --without-gnu-ld
--without-gnu-as --enable-shared
Thread model: posix
gcc version 3.3.2
g++ -ansi -pedantic -Wall -o stest5_test_00.out stest5_test_00.cpp -L
/opt/sfw/gcc-3/lib/ -R /opt/sfw/gcc-3/lib/ -lstdc++
stest5_test_00.cpp:11: error: syntax error before `;' token
#include <iostream>
#include <string>
#include <cstdlib>
// #include "stack5.hpp"
template <typename T>
class Stack {
private:
std::deque<T> elems; // elements
public:
void push(const T&); // store new top element
void pop(); // remove top element
T top() const; // return top element
bool empty() const { // return whether the stack is empty
return elems.empty();
}
// assign stack of elements of type T2
template <typename T2>
Stack<T>& operator= (Stack<T2> const&);
};
template <typename T>
template <typename T2>
Stack<T>& Stack<T>::operator= (const Stack<T2>& op2)
{
if ((void*)this == (void*)&op2) { // assignment to itself?
return *this;
}
Stack<T2> tmp(op2); // create a copy of the assigned stack
elems.clear(); // remove existing elements
while (!tmp.empty()) { // copy all elements
elems.push_front(tmp.top());
tmp.pop();
}
return *this;
}
int main()
{
try {
Stack<int> intStack; // stack of ints
Stack<float> floatStack; // stack of floats
// manipulate int stack
intStack.push(42);
intStack.push(7);
// manipulate float stack
floatStack.push(7.7);
// assign stacks of different type
floatStack = intStack;
// print float stack
std::cout << floatStack.top() << std::endl;
floatStack.pop();
std::cout << floatStack.top() << std::endl;
floatStack.pop();
std::cout << floatStack.top() << std::endl;
floatStack.pop();
}
catch (std::exception const& ex) {
std::cerr << "Exception: " << ex.what() << std::endl;
return EXIT_FAILURE; // exit program with ERROR status
}
}
best regards
Morten
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: template under gcc version 3.3.2
2005-01-29 13:42 template under gcc version 3.3.2 Morten.Gulbrandsen
@ 2005-01-29 20:59 ` corey taylor
2005-01-29 22:33 ` Morten Gulbrandsen
0 siblings, 1 reply; 5+ messages in thread
From: corey taylor @ 2005-01-29 20:59 UTC (permalink / raw)
To: Morten.Gulbrandsen, gcc-help
Morten,
First one note, I don't see you including anything for the std::deque type.
You should at least add: #include <deque>
corey
> g++ -ansi -pedantic -Wall -o stest5_test_00.out stest5_test_00.cpp -L
> /opt/sfw/gcc-3/lib/ -R /opt/sfw/gcc-3/lib/ -lstdc++
> stest5_test_00.cpp:11: error: syntax error before `;' token
>
> #include <iostream>
> #include <string>
> #include <cstdlib>
>
> // #include "stack5.hpp"
>
> template <typename T>
> class Stack {
> private:
> std::deque<T> elems; // elements
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: template under gcc version 3.3.2
2005-01-29 20:59 ` corey taylor
@ 2005-01-29 22:33 ` Morten Gulbrandsen
2005-01-29 22:44 ` corey taylor
0 siblings, 1 reply; 5+ messages in thread
From: Morten Gulbrandsen @ 2005-01-29 22:33 UTC (permalink / raw)
To: corey taylor; +Cc: gcc-help
corey taylor wrote:
>Morten,
>
> First one note, I don't see you including anything for the std::deque type.
>
> You should at least add: #include <deque>
>
>corey
>
>
>
>>g++ -ansi -pedantic -Wall -o stest5_test_00.out stest5_test_00.cpp -L
>>/opt/sfw/gcc-3/lib/ -R /opt/sfw/gcc-3/lib/ -lstdc++
>>stest5_test_00.cpp:11: error: syntax error before `;' token
>>
>>#include <iostream>
>>#include <string>
>>#include <cstdlib>
>>
>>// #include "stack5.hpp"
>>
>>template <typename T>
>>class Stack {
>> private:
>> std::deque<T> elems; // elements
>>
>>
>
>
>
OK I did,
The preceeding code files are all good,
I also tried www.comeaucomputing.com
#include <iostream>
#include <string>
#include <cstdlib>
#include <deque>
#include <vector>
#include <stdexcept>
// #include "stack5.hpp"
// these files are unchanged
#include "stack5decl.hpp"
#include "stack5assign.hpp"
int main()
{
try {
Stack<int> intStack; // stack of ints
Stack<float> floatStack; // stack of floats
// manipulate int stack
intStack.push(42);
intStack.push(7);
// manipulate float stack
floatStack.push(7.7);
// assign stacks of different type
floatStack = intStack;
// print float stack
std::cout << floatStack.top() << std::endl;
floatStack.pop();
std::cout << floatStack.top() << std::endl;
floatStack.pop();
std::cout << floatStack.top() << std::endl;
floatStack.pop();
}
catch (std::exception const& ex) {
std::cerr << "Exception: " << ex.what() << std::endl;
return EXIT_FAILURE; // exit program with ERROR status
}
}
/*
g++ -o stest5.out stest5.cpp -L /opt/sfw/gcc-3/lib -R /opt/sfw/gcc-3/lib
-lstdc++
Undefined first referenced
symbol in file
Stack<int>::push(int const&) /var/tmp//ccy3WHcF.o
Stack<float>::push(float const&) /var/tmp//ccy3WHcF.o
Stack<int>::pop() /var/tmp//ccy3WHcF.o
Stack<float>::pop() /var/tmp//ccy3WHcF.o
Stack<float>::top() const /var/tmp//ccy3WHcF.o
Stack<int>::top() const /var/tmp//ccy3WHcF.o
ld: fatal: Symbol referencing errors. No output written to stest5.out
collect2: ld returned 1 exit status
g++ -v -o stest5.out stest5.cpp -L /opt/sfw/gcc-3/lib -R
/opt/sfw/gcc-3/lib -lstdc++
Reading specs from /opt/sfw/gcc-3/lib/gcc-lib/i386-pc-solaris2.9/3.3.2/specs
Configured with: ../gcc-3.3.2/configure --prefix=/opt/sfw/gcc-3
--with-ld=/usr/ccs/bin/ld --with-as=/usr/ccs/bin/as --without-gnu-ld
--without-gnu-as --enable-shared
Thread model: posix
gcc version 3.3.2
/opt/sfw/gcc-3/lib/gcc-lib/i386-pc-solaris2.9/3.3.2/cc1plus -quiet -v
-D__GNUC__=3 -D__GNUC_MINOR__=3 -D__GNUC_PATCHLEVEL__=2 stest5.cpp
-D__GNUG__=3 -quiet -dumpbase stest5.cpp -auxbase stest5 -version -o
/var/tmp//ccDX6SkL.s
GNU C++ version 3.3.2 (i386-pc-solaris2.9)
compiled by GNU C version 3.3.2.
GGC heuristics: --param ggc-min-expand=47 --param ggc-min-heapsize=32718
ignoring nonexistent directory "/usr/local/include"
ignoring nonexistent directory "/opt/sfw/gcc-3/i386-pc-solaris2.9/include"
#include "..." search starts here:
#include <...> search starts here:
/opt/sfw/gcc-3/include/c++/3.3.2
/opt/sfw/gcc-3/include/c++/3.3.2/i386-pc-solaris2.9
/opt/sfw/gcc-3/include/c++/3.3.2/backward
/opt/sfw/gcc-3/include
/opt/sfw/gcc-3/lib/gcc-lib/i386-pc-solaris2.9/3.3.2/include
/usr/include
End of search list.
/usr/ccs/bin/as -V -Qy -s -o /var/tmp//cc7CpDtM.o /var/tmp//ccDX6SkL.s
as: Sun Compiler Common 9.0 Patch 115114-02 2004/02/09
/opt/sfw/gcc-3/lib/gcc-lib/i386-pc-solaris2.9/3.3.2/collect2 -V -R
/opt/sfw/gcc-3/lib -Y P,/usr/ccs/lib:/usr/lib -Qy -o stest5.out
/opt/sfw/gcc-3/lib/gcc-lib/i386-pc-solaris2.9/3.3.2/crt1.o
/opt/sfw/gcc-3/lib/gcc-lib/i386-pc-solaris2.9/3.3.2/crti.o
/usr/ccs/lib/values-Xa.o
/opt/sfw/gcc-3/lib/gcc-lib/i386-pc-solaris2.9/3.3.2/crtbegin.o -L
/opt/sfw/gcc-3/lib -L/opt/sfw/gcc-3/lib/gcc-lib/i386-pc-solaris2.9/3.3.2
-L/usr/ccs/bin -L/usr/ccs/lib
-L/opt/sfw/gcc-3/lib/gcc-lib/i386-pc-solaris2.9/3.3.2/../../..
/var/tmp//cc7CpDtM.o -lstdc++ -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s
-lgcc /opt/sfw/gcc-3/lib/gcc-lib/i386-pc-solaris2.9/3.3.2/crtend.o
/opt/sfw/gcc-3/lib/gcc-lib/i386-pc-solaris2.9/3.3.2/crtn.o
ld: Software Generation Utilities - Solaris Link Editors: 5.9-1.385
Undefined first referenced
symbol in file
Stack<int>::push(int const&) /var/tmp//cc7CpDtM.o
Stack<float>::push(float const&) /var/tmp//cc7CpDtM.o
Stack<int>::pop() /var/tmp//cc7CpDtM.o
Stack<float>::pop() /var/tmp//cc7CpDtM.o
Stack<float>::top() const /var/tmp//cc7CpDtM.o
Stack<int>::top() const /var/tmp//cc7CpDtM.o
ld: fatal: Symbol referencing errors. No output written to stest5.out
collect2: ld returned 1 exit status
*/
best regards
Morten
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: template under gcc version 3.3.2
2005-01-29 22:33 ` Morten Gulbrandsen
@ 2005-01-29 22:44 ` corey taylor
2005-01-30 12:43 ` template under gcc version 3.3.2 *** SOLVED*** Morten Gulbrandsen
0 siblings, 1 reply; 5+ messages in thread
From: corey taylor @ 2005-01-29 22:44 UTC (permalink / raw)
To: Morten Gulbrandsen, gcc-help
Morten,
Well, if those are all the files you have, then the issue is simply
that you never implemented the Stack class methods that gcc throws out
as "undefined references."
Only the method definitions are there which allow a compilation,
however the linker will error trying to connect the symbols to actual
code.
corey
On Sat, 29 Jan 2005 23:34:00 +0100, Morten Gulbrandsen
<f1000mhz@yahoo.de> wrote:
> corey taylor wrote:
>
> >Morten,
> >
> > First one note, I don't see you including anything for the std::deque type.
> >
> > You should at least add: #include <deque>
> >
> >corey
> >
> >
> >
> >>g++ -ansi -pedantic -Wall -o stest5_test_00.out stest5_test_00.cpp -L
> >>/opt/sfw/gcc-3/lib/ -R /opt/sfw/gcc-3/lib/ -lstdc++
> >>stest5_test_00.cpp:11: error: syntax error before `;' token
> >>
> >>#include <iostream>
> >>#include <string>
> >>#include <cstdlib>
> >>
> >>// #include "stack5.hpp"
> >>
> >>template <typename T>
> >>class Stack {
> >> private:
> >> std::deque<T> elems; // elements
> >>
> >>
> >
> >
> >
> OK I did,
>
> The preceeding code files are all good,
>
> I also tried www.comeaucomputing.com
>
>
> #include <iostream>
> #include <string>
> #include <cstdlib>
>
> #include <deque>
> #include <vector>
> #include <stdexcept>
>
> // #include "stack5.hpp"
>
> // these files are unchanged
> #include "stack5decl.hpp"
> #include "stack5assign.hpp"
>
>
> int main()
> {
> try {
> Stack<int> intStack; // stack of ints
> Stack<float> floatStack; // stack of floats
>
> // manipulate int stack
> intStack.push(42);
> intStack.push(7);
>
> // manipulate float stack
> floatStack.push(7.7);
>
> // assign stacks of different type
> floatStack = intStack;
>
> // print float stack
> std::cout << floatStack.top() << std::endl;
> floatStack.pop();
> std::cout << floatStack.top() << std::endl;
> floatStack.pop();
> std::cout << floatStack.top() << std::endl;
> floatStack.pop();
> }
> catch (std::exception const& ex) {
> std::cerr << "Exception: " << ex.what() << std::endl;
> return EXIT_FAILURE; // exit program with ERROR status
> }
> }
>
>
> /*
>
> g++ -o stest5.out stest5.cpp -L /opt/sfw/gcc-3/lib -R /opt/sfw/gcc-3/lib
> -lstdc++
>
> Undefined first referenced
> symbol in file
> Stack<int>::push(int const&) /var/tmp//ccy3WHcF.o
> Stack<float>::push(float const&) /var/tmp//ccy3WHcF.o
> Stack<int>::pop() /var/tmp//ccy3WHcF.o
> Stack<float>::pop() /var/tmp//ccy3WHcF.o
> Stack<float>::top() const /var/tmp//ccy3WHcF.o
> Stack<int>::top() const /var/tmp//ccy3WHcF.o
> ld: fatal: Symbol referencing errors. No output written to stest5.out
> collect2: ld returned 1 exit status
>
> g++ -v -o stest5.out stest5.cpp -L /opt/sfw/gcc-3/lib -R
> /opt/sfw/gcc-3/lib -lstdc++
> Reading specs from /opt/sfw/gcc-3/lib/gcc-lib/i386-pc-solaris2.9/3.3.2/specs
> Configured with: ../gcc-3.3.2/configure --prefix=/opt/sfw/gcc-3
> --with-ld=/usr/ccs/bin/ld --with-as=/usr/ccs/bin/as --without-gnu-ld
> --without-gnu-as --enable-shared
> Thread model: posix
> gcc version 3.3.2
> /opt/sfw/gcc-3/lib/gcc-lib/i386-pc-solaris2.9/3.3.2/cc1plus -quiet -v
> -D__GNUC__=3 -D__GNUC_MINOR__=3 -D__GNUC_PATCHLEVEL__=2 stest5.cpp
> -D__GNUG__=3 -quiet -dumpbase stest5.cpp -auxbase stest5 -version -o
> /var/tmp//ccDX6SkL.s
> GNU C++ version 3.3.2 (i386-pc-solaris2.9)
> compiled by GNU C version 3.3.2.
> GGC heuristics: --param ggc-min-expand=47 --param ggc-min-heapsize=32718
> ignoring nonexistent directory "/usr/local/include"
> ignoring nonexistent directory "/opt/sfw/gcc-3/i386-pc-solaris2.9/include"
> #include "..." search starts here:
> #include <...> search starts here:
> /opt/sfw/gcc-3/include/c++/3.3.2
> /opt/sfw/gcc-3/include/c++/3.3.2/i386-pc-solaris2.9
> /opt/sfw/gcc-3/include/c++/3.3.2/backward
> /opt/sfw/gcc-3/include
> /opt/sfw/gcc-3/lib/gcc-lib/i386-pc-solaris2.9/3.3.2/include
> /usr/include
> End of search list.
> /usr/ccs/bin/as -V -Qy -s -o /var/tmp//cc7CpDtM.o /var/tmp//ccDX6SkL.s
> as: Sun Compiler Common 9.0 Patch 115114-02 2004/02/09
> /opt/sfw/gcc-3/lib/gcc-lib/i386-pc-solaris2.9/3.3.2/collect2 -V -R
> /opt/sfw/gcc-3/lib -Y P,/usr/ccs/lib:/usr/lib -Qy -o stest5.out
> /opt/sfw/gcc-3/lib/gcc-lib/i386-pc-solaris2.9/3.3.2/crt1.o
> /opt/sfw/gcc-3/lib/gcc-lib/i386-pc-solaris2.9/3.3.2/crti.o
> /usr/ccs/lib/values-Xa.o
> /opt/sfw/gcc-3/lib/gcc-lib/i386-pc-solaris2.9/3.3.2/crtbegin.o -L
> /opt/sfw/gcc-3/lib -L/opt/sfw/gcc-3/lib/gcc-lib/i386-pc-solaris2.9/3.3.2
> -L/usr/ccs/bin -L/usr/ccs/lib
> -L/opt/sfw/gcc-3/lib/gcc-lib/i386-pc-solaris2.9/3.3.2/../../..
> /var/tmp//cc7CpDtM.o -lstdc++ -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s
> -lgcc /opt/sfw/gcc-3/lib/gcc-lib/i386-pc-solaris2.9/3.3.2/crtend.o
> /opt/sfw/gcc-3/lib/gcc-lib/i386-pc-solaris2.9/3.3.2/crtn.o
> ld: Software Generation Utilities - Solaris Link Editors: 5.9-1.385
> Undefined first referenced
> symbol in file
> Stack<int>::push(int const&) /var/tmp//cc7CpDtM.o
> Stack<float>::push(float const&) /var/tmp//cc7CpDtM.o
> Stack<int>::pop() /var/tmp//cc7CpDtM.o
> Stack<float>::pop() /var/tmp//cc7CpDtM.o
> Stack<float>::top() const /var/tmp//cc7CpDtM.o
> Stack<int>::top() const /var/tmp//cc7CpDtM.o
> ld: fatal: Symbol referencing errors. No output written to stest5.out
> collect2: ld returned 1 exit status
>
> */
>
> best regards
>
> Morten
>
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: template under gcc version 3.3.2 *** SOLVED***
2005-01-29 22:44 ` corey taylor
@ 2005-01-30 12:43 ` Morten Gulbrandsen
0 siblings, 0 replies; 5+ messages in thread
From: Morten Gulbrandsen @ 2005-01-30 12:43 UTC (permalink / raw)
To: corey taylor; +Cc: gcc-help
corey taylor wrote:
>Morten,
>
> Well, if those are all the files you have, then the issue is simply
>that you never implemented the Stack class methods that gcc throws out
>as "undefined references."
>
> Only the method definitions are there which allow a compilation,
>however the linker will error trying to connect the symbols to actual
>code.
>
>corey
>
>
Thanks,
actually something was missing. not only a small typo.
I searched and found it here.
http://www.josuttis.com/tmplbook/basics/stack5test.cpp.html
the missing piece is
http://www.josuttis.com/tmplbook/basics/stack5.hpp.html
#ifndef STACK_HPP
#define STACK_HPP
#include <deque>
#include <stdexcept>
#include "stack5decl.hpp
<http://www.josuttis.com/tmplbook/basics/stack5decl.hpp.html>"
#include "stack5assign.hpp
<http://www.josuttis.com/tmplbook/basics/stack5assign.hpp.html>"
template <typename T>
void Stack<T>::push (T const& elem)
{
elems.push_back(elem); /// append copy of passed elem/
}
template<typename T>
void Stack<T>::pop ()
{
if (elems.empty()) {
throw std::out_of_range("Stack<>::pop(): empty stack");
}
elems.pop_back(); /// remove last element/
}
template <typename T>
T Stack<T>::top () const
{
if (elems.empty()) {
throw std::out_of_range("Stack<>::top(): empty stack");
}
return elems.back(); /// return copy of last element/
}
#endif /// STACK_HPP
/
it compiles and runs fine
bash-2.05$ g++ -ansi -pedantic -Wall -o stack5test.out stack5test.cpp -L
/opt/sfw/gcc-3/lib -R /opt/sfw/gcc-3/lib -lstdc++
bash-2.05$ ./stack5test.out
7
42
Exception: Stack<>::top(): empty stack
No complains from gcc,
g++ -v
Reading specs from /opt/sfw/gcc-3/lib/gcc-lib/i386-pc-solaris2.9/3.3.2/specs
Configured with: ../gcc-3.3.2/configure --prefix=/opt/sfw/gcc-3
--with-ld=/usr/ccs/bin/ld --with-as=/usr/ccs/bin/as --without-gnu-ld
--without-gnu-as --enable-shared
Thread model: posix
gcc version 3.3.2
Best regards
Morten
/
/
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2005-01-30 12:43 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-01-29 13:42 template under gcc version 3.3.2 Morten.Gulbrandsen
2005-01-29 20:59 ` corey taylor
2005-01-29 22:33 ` Morten Gulbrandsen
2005-01-29 22:44 ` corey taylor
2005-01-30 12:43 ` template under gcc version 3.3.2 *** SOLVED*** Morten Gulbrandsen
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).