Skip to main content



      Home
Home » Language IDEs » C / C++ IDE (CDT) » Using templates in CDT/Linux/g++
Using templates in CDT/Linux/g++ [message #121687] Sun, 26 September 2004 16:57 Go to next message
Eclipse UserFriend
Originally posted by: hma5.nospam.edu

This may be more of a g++ issue, but can anyone give me tips for working
with code that has C++ templates?

CDT in Linux/GTK uses G++ by default so templates defined in
"Box.h/Box.cpp" and used in "MyApp.cpp" gives "undefined reference to
Box<int>..." errors. (Sample code below)

Is there a coding change or a flag I can set in Eclipse on the compiler
to make this work?

Thanks!

Horus

-----------
Box.h
-----------
template<typename Type>
class Box{
private:
int n;
Type * _value;
public:
Box();
void setValue(Type * );
Type* getValue();
};

-----------
Box.cpp
-----------
#include "Box.h"
template<typename Type>
Box<Type>::Box()
{}
template<typename Type>
void Box<Type>::setValue(Type * v)
{
_value = v;
}
template<typename Type>
Type * Box<Type>::getValue()
{
return _value;
}

------------
MyApp.cpp
------------
#include <iostream>
#include "Box.h"
#include <string>

using std::cout;
using std::endl;
using std::string;

int main()
{
Box<int> * b;
b = new Box<int>();
int * val = new int(3);
b->setValue(val);
cout << "The value in box is " << *(b->getValue()) << endl;

return 0;
}
Re: Using templates in CDT/Linux/g++ [message #121703 is a reply to message #121687] Sun, 26 September 2004 21:03 Go to previous message
Eclipse UserFriend
http://babbage.cs.qc.edu/STL_Docs/templates.htm#T3

Implementing class template member functions

Implementing template member functions is somewhat different compared to
the regular class member functions. The declarations and definitions of
the class template member functions should all be in the same header file.

Horus M Alkebulan wrote:
> This may be more of a g++ issue, but can anyone give me tips for working
> with code that has C++ templates?
>
> CDT in Linux/GTK uses G++ by default so templates defined in
> "Box.h/Box.cpp" and used in "MyApp.cpp" gives "undefined reference to
> Box<int>..." errors. (Sample code below)
>
> Is there a coding change or a flag I can set in Eclipse on the compiler
> to make this work?
>
> Thanks!
>
> Horus
>
> -----------
> Box.h
> -----------
> template<typename Type>
> class Box{
> private:
> int n;
> Type * _value;
> public:
> Box();
> void setValue(Type * );
> Type* getValue();
> };
>
> -----------
> Box.cpp
> -----------
> #include "Box.h"
> template<typename Type>
> Box<Type>::Box()
> {}
> template<typename Type>
> void Box<Type>::setValue(Type * v)
> {
> _value = v;
> }
> template<typename Type>
> Type * Box<Type>::getValue()
> {
> return _value;
> }
>
> ------------
> MyApp.cpp
> ------------
> #include <iostream>
> #include "Box.h"
> #include <string>
>
> using std::cout;
> using std::endl;
> using std::string;
>
> int main()
> {
> Box<int> * b;
> b = new Box<int>();
> int * val = new int(3);
> b->setValue(val);
> cout << "The value in box is " << *(b->getValue()) << endl;
>
> return 0;
> }
>
Previous Topic:C++ Properties pages
Next Topic:Indexing problems
Goto Forum:
  


Current Time: Tue Jul 22 19:50:37 EDT 2025

Powered by FUDForum. Page generated in 0.03901 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top