473,624 Members | 2,612 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help on a template question

Hi, everyone, I started learning C++ two weeks ago. I am trying to
play with template with constant expressions for template. So I wrote
a very simple program as follows... There are three files: gfield.h,
gfield.cpp and main.c.
//////////////////////////////////////
// gfield.h

#pragma once
#include <iostream>
using namespace std;

template <int n>
class gfield
{
public:
explicit gfield(int x):_num(x%n){}
~gfield(void);
int get_value(void) const {return _num;}
private:

int _num;
};

template <int n>
ostream& operator <<(ostream& os,const gfield<n>& gf);

///////////////////////////
//gfield.cpp

#include "gfield.h"
#include <iostream>

using namespace std;

template <int n>
gfield<n>::~gfi eld(void)
{

}

template <int n>
ostream& operator <<(ostream& os,const gfield<n>& gf)
{
os<<gf.get_valu e()<<endl;
return os;
}
///////////////////////
// main.cpp

#include <cstdlib>
#include <string>
#include <cstdio>
#include <iostream>
#include "gfield.h"

using namespace std;
main()
{
gfield<2gf(3);
cout<<gf;

cout<<test_retu rn();
}

The problem I met is that there will be link error : unresolved
external symbol... Basically, the compiler could not find operator <<
and gfield<n>::~gfi eld(void). However, if I move the definition of
these two function definitions to either main.cpp or gfield.h, it
would work.

Can anyone help me on this? Thank you very much!

Jul 7 '07 #1
3 1365
On Jul 7, 7:05 pm, "jianqi.w...@gm ail.com" <jianqi.w...@gm ail.com>
wrote:
Hi, everyone, I started learning C++ two weeks ago. I am trying to
play with template with constant expressions for template. So I wrote
a very simple program as follows... There are three files: gfield.h,
gfield.cpp and main.c.
//////////////////////////////////////
// gfield.h

#pragma once
#include <iostream>
using namespace std;

template <int n>
class gfield
{
public:
explicit gfield(int x):_num(x%n){}
~gfield(void);
int get_value(void) const {return _num;}
private:

int _num;

};

template <int n>
ostream& operator <<(ostream& os,const gfield<n>& gf);

///////////////////////////
//gfield.cpp

#include "gfield.h"
#include <iostream>

using namespace std;

template <int n>
gfield<n>::~gfi eld(void)
{

}

template <int n>
ostream& operator <<(ostream& os,const gfield<n>& gf)
{
os<<gf.get_valu e()<<endl;
return os;

}

///////////////////////
// main.cpp

#include <cstdlib>
#include <string>
#include <cstdio>
#include <iostream>
#include "gfield.h"

using namespace std;

main()
{
gfield<2gf(3);
cout<<gf;

cout<<test_retu rn();

}

The problem I met is that there will be link error : unresolved
external symbol... Basically, the compiler could not find operator <<
and gfield<n>::~gfi eld(void). However, if I move the definition of
these two function definitions to either main.cpp or gfield.h, it
would work.

Can anyone help me on this? Thank you very much!
Please ignore that cout<<test_retu rn(), I was just doing test....
Thank you.

Jul 7 '07 #2
On 2007-07-08 01:05, ji*********@gma il.com wrote:
Hi, everyone, I started learning C++ two weeks ago. I am trying to
play with template with constant expressions for template. So I wrote
a very simple program as follows... There are three files: gfield.h,
gfield.cpp and main.c.
//////////////////////////////////////
// gfield.h

#pragma once
You should use proper include guards to make the code portable.
#include <iostream>
using namespace std;
Don't use this in a header-file, it can cause unexpected behaviour and
hard to find errors.
template <int n>
class gfield
{
public:
explicit gfield(int x):_num(x%n){}
~gfield(void);
int get_value(void) const {return _num;}
private:

int _num;
};

template <int n>
ostream& operator <<(ostream& os,const gfield<n>& gf);

///////////////////////////
//gfield.cpp

#include "gfield.h"
#include <iostream>

using namespace std;

template <int n>
gfield<n>::~gfi eld(void)
{

}

template <int n>
ostream& operator <<(ostream& os,const gfield<n>& gf)
{
os<<gf.get_valu e()<<endl;
return os;
}
///////////////////////
// main.cpp

#include <cstdlib>
Don't need this one.
#include <string>
#include <cstdio>
Or this.
#include <iostream>
#include "gfield.h"

using namespace std;
main()
{
gfield<2gf(3);
cout<<gf;

cout<<test_retu rn();
Didn't include this function in the code. Try to always post compilable
(and if possible working) code.
}

The problem I met is that there will be link error : unresolved
external symbol... Basically, the compiler could not find operator <<
and gfield<n>::~gfi eld(void). However, if I move the definition of
these two function definitions to either main.cpp or gfield.h, it
would work.

Can anyone help me on this? Thank you very much!
Yes, this is a common problem with templates, in theory you should be
able to use the export keyword to solve this, but few compiler support
it. The solution is to put all the code in the header file. See also:
http://www.parashift.com/c++-faq-lit...html#faq-35.12

--
Erik Wikström
Jul 7 '07 #3
On Jul 7, 7:21 pm, Erik Wikström <Erik-wikst...@telia. comwrote:
On 2007-07-08 01:05, jianqi.w...@gma il.com wrote:
Hi, everyone, I started learning C++ two weeks ago. I am trying to
play with template with constant expressions for template. So I wrote
a very simple program as follows... There are three files: gfield.h,
gfield.cpp and main.c.
//////////////////////////////////////
// gfield.h
#pragma once

You should use proper include guards to make the code portable.
#include <iostream>
using namespace std;

Don't use this in a header-file, it can cause unexpected behaviour and
hard to find errors.
template <int n>
class gfield
{
public:
explicit gfield(int x):_num(x%n){}
~gfield(void);
int get_value(void) const {return _num;}
private:
int _num;
};
template <int n>
ostream& operator <<(ostream& os,const gfield<n>& gf);
///////////////////////////
//gfield.cpp
#include "gfield.h"
#include <iostream>
using namespace std;
template <int n>
gfield<n>::~gfi eld(void)
{
}
template <int n>
ostream& operator <<(ostream& os,const gfield<n>& gf)
{
os<<gf.get_valu e()<<endl;
return os;
}
///////////////////////
// main.cpp
#include <cstdlib>

Don't need this one.
#include <string>
#include <cstdio>

Or this.
#include <iostream>
#include "gfield.h"
using namespace std;
main()
{
gfield<2gf(3);
cout<<gf;
cout<<test_retu rn();

Didn't include this function in the code. Try to always post compilable
(and if possible working) code.
}
The problem I met is that there will be link error : unresolved
external symbol... Basically, the compiler could not find operator <<
and gfield<n>::~gfi eld(void). However, if I move the definition of
these two function definitions to either main.cpp or gfield.h, it
would work.
Can anyone help me on this? Thank you very much!

Yes, this is a common problem with templates, in theory you should be
able to use the export keyword to solve this, but few compiler support
it. The solution is to put all the code in the header file. See also:http://www.parashift.com/c++-faq-lit...html#faq-35.12

--
Erik Wikström
Erik, thank you very much! It really helps.

Jul 8 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3874
by: Bernard | last post by:
Hi, I have a problem with a CGI script (Perl) on a Win2000 server. The script is for sending E-cards and was written by Jason Maloney: http://www.aestheticsurgerycenter.com/scripts/postcard/postcard1.shtml You can choose from some cards, add your own text and send it to a friend. This friend get a mail which tell him/her that he/she has a mail and have to click a link. So far everything works just fine. But when the recipient clicks...
6
2010
by: Dave | last post by:
Hello all, Consider this function template definition: template<typename T> void foo(T) {} If foo is never called, this template will never be instantiated. Now consider this explicit instantiation of foo:
10
2088
by: Suki | last post by:
Hi, I'm writing a templated class, and i dont want to use the class otherthan for some predetermined types, say, int, double etc. This class has no meaning for typenames other than those few. ========================= template <class T> myClass {....} ;
1
1303
by: William Gower | last post by:
I need to develop a datagrid that uses columns from a table. In addition I need two columns (checkboxes) that the user will use to indicate that this record can be closed later. The checkboxed column is not a field in the table. It is just for the purposes of the Datagrid and I do not need to store whether it was checked or not. Do I use a Template Column for this?
22
2117
by: macAWM | last post by:
Hi list, First let me explain that my background is in Java and I am quite spoiled to its niceties (read "less ambiguous nature"). Anyway to my problems. 1. I want to write my own library for what I consider to be some holes in the standard language. How do I go about writing and compiling this without this stupid error about not having a 'main' function. I don't want a stupid 'main' function. (I'm compiling using gcc 4.0.) I would...
3
2446
by: vijaykokate | last post by:
Our company http://www.softnmation.com/ offers its customers a great variety of products. Everything you need can be found in this site. Web Template, CSS Template, Logo Template, Corporate Identity Package, Logo Set,Icon Set, Flash Animated Template, Flash Intro Header, Flash Site, PHP-Nuke Theme, Full Site, Stretched Web Templates and Stretched Flash Templates, ZenCart Templates ,osCommerce Templates and many more
45
2882
by: charles.lobo | last post by:
Hi, I have recently begun using templates in C++ and have found it to be quite useful. However, hearing stories of code bloat and assorted problems I decided to write a couple of small programs to check. What I expected was that there would be minor code bloat and some speed improvement when using templates. However... I wrote a basic list container (using templates), and a list container (using virtual derived classes). I also tried...
2
2587
by: aitrob | last post by:
Hi, I have a problem concerning templates/inheritance. I have a code that compiles fine with g++ 4.0.1 (Apple version), but gives a lot of errors with Intel C++ 10.1 (Mac OS X). I'm not sure if I'm doing something wrong and g++ just doesn't notice, or if the people at Intel are doing something weird... What am I trying to do? I'm trying to implement a template class that inherits from the Blitz++ array library #include <blitz/array.h>...
3
1846
by: stdlib99 | last post by:
Hi, I have a simple question regarding templates and meta programming. I am going to try and work my way through the C++ Template Metaprogramming, a book by David Abrahams and Aleksey Gurtovoy. I’m not doing this because I want to be a Meta Programming guru (because a lot of that stuff looks too crazy for use in the real world). Rather I want to learn heavyweight templates and this is the only
0
8688
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8635
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7178
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6115
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4085
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4188
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2614
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1800
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1496
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.