Need help on a template question

J

jianqi.wang

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<2> gf(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!
 
J

jianqi.wang

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<2> gf(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.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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<2> gf(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-lite/templates.html#faq-35.12
 
J

jianqi.wang

You should use proper include guards to make the code portable.


Don't use this in a header-file, it can cause unexpected behaviour and
hard to find errors.










Don't need this one.


Or this.





Didn't include this function in the code. Try to always post compilable
(and if possible working) code.




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-lite/templates.html#faq-35.12

Erik, thank you very much! It really helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,125
Latest member
VinayKumar Nevatia_
Top