473,499 Members | 1,562 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>::~gfield(void)
{

}

template <int n>
ostream& operator <<(ostream& os,const gfield<n>& gf)
{
os<<gf.get_value()<<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_return();
}

The problem I met is that there will be link error : unresolved
external symbol... Basically, the compiler could not find operator <<
and gfield<n>::~gfield(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 1358
On Jul 7, 7:05 pm, "jianqi.w...@gmail.com" <jianqi.w...@gmail.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>::~gfield(void)
{

}

template <int n>
ostream& operator <<(ostream& os,const gfield<n>& gf)
{
os<<gf.get_value()<<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_return();

}

The problem I met is that there will be link error : unresolved
external symbol... Basically, the compiler could not find operator <<
and gfield<n>::~gfield(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_return(), I was just doing test....
Thank you.

Jul 7 '07 #2
On 2007-07-08 01:05, ji*********@gmail.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>::~gfield(void)
{

}

template <int n>
ostream& operator <<(ostream& os,const gfield<n>& gf)
{
os<<gf.get_value()<<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_return();
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>::~gfield(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...@gmail.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>::~gfield(void)
{
}
template <int n>
ostream& operator <<(ostream& os,const gfield<n>& gf)
{
os<<gf.get_value()<<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_return();

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>::~gfield(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
3869
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:...
6
2004
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...
10
2071
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. ...
1
1296
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...
22
2098
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...
3
2430
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...
45
2840
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...
2
2563
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...
3
1836
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...
0
7134
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7012
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6901
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7392
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4920
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...
0
3105
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...
0
1429
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 ...
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
307
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...

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.