473,414 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

More templates

Hi All,

In the stripped down code example below Visual C++ 2005 issues the following
error:

error LNK2019: unresolved external symbol "public: void __thiscall
Addsub<24>::do_process123(void)"

This is a SystemC example, however I believe this is a basic C++ syntax
error since if I move the Addsub function inside the Addsub class (that is
delete File2) then all is OK so I must have made a mistake in the template
definition?

Thanks,
Hans.

// File 1 addsub.h
template <int ADDSUB_WIDTH=24>
SC_MODULE(Addsub)
{

void do_process123(); // fails
void do_process123() {}; // works, excl addsub.cpp

SC_CTOR(Addsub) {
SC_METHOD(do_process123);
}
};

// File 2 addsub.cpp
template <int ADDSUB_WIDTH>
void Addsub<ADDSUB_WIDTH>::do_process123(){}

// File 3 top.h
#include "addsub.h"

#define WIDTH 24

SC_MODULE(Cordic) {
public:

Addsub<WIDTHADD1;

void do_process1() { }
SC_CTOR(Cordic) : ADD1("ADD1") {
SC_METHOD(do_process1);
}
};
Jul 18 '06 #1
9 1770
Hans wrote:
Hi All,

In the stripped down code example below Visual C++ 2005 issues the following
error:

error LNK2019: unresolved external symbol "public: void __thiscall
Addsub<24>::do_process123(void)"

This is a SystemC example, however I believe this is a basic C++ syntax
error since if I move the Addsub function inside the Addsub class (that is
delete File2) then all is OK so I must have made a mistake in the template
definition?

Thanks,
Hans.

// File 1 addsub.h
template <int ADDSUB_WIDTH=24>
SC_MODULE(Addsub)
{

void do_process123(); // fails
void do_process123() {}; // works, excl addsub.cpp

SC_CTOR(Addsub) {
SC_METHOD(do_process123);
}
};

// File 2 addsub.cpp
template <int ADDSUB_WIDTH>
void Addsub<ADDSUB_WIDTH>::do_process123(){}

// File 3 top.h
#include "addsub.h"

#define WIDTH 24

SC_MODULE(Cordic) {
public:

Addsub<WIDTHADD1;

void do_process1() { }
SC_CTOR(Cordic) : ADD1("ADD1") {
SC_METHOD(do_process1);
}
};
Unless your compiler supports the export keyword (it probably doesn't),
then Addsub() must be defined in each translation unit that uses it.
The easiest way to accomplish this is to make sure it appears in the
header file, whether in the class definition or by #including
addsub.cpp at the end of the header file.

Cheers! --M

Jul 18 '06 #2

Hans wrote:
Hi All,

In the stripped down code example below Visual C++ 2005 issues the following
error:

error LNK2019: unresolved external symbol "public: void __thiscall
Addsub<24>::do_process123(void)"

This is a SystemC example, however I believe this is a basic C++ syntax
error since if I move the Addsub function inside the Addsub class (that is
delete File2) then all is OK so I must have made a mistake in the template
definition?

Thanks,
Hans.

// File 1 addsub.h
template <int ADDSUB_WIDTH=24>
SC_MODULE(Addsub)
{

void do_process123(); // fails
void do_process123() {}; // works, excl addsub.cpp

SC_CTOR(Addsub) {
SC_METHOD(do_process123);
}
};

// File 2 addsub.cpp
template <int ADDSUB_WIDTH>
void Addsub<ADDSUB_WIDTH>::do_process123(){}

// File 3 top.h
#include "addsub.h"

#define WIDTH 24

SC_MODULE(Cordic) {
public:

Addsub<WIDTHADD1;

void do_process1() { }
SC_CTOR(Cordic) : ADD1("ADD1") {
SC_METHOD(do_process1);
}
};
This is an FAQ:

http://www.parashift.com/c++-faq-lit...html#faq-35.12

Best regards,

Tom

Jul 18 '06 #3
This is an FAQ:
>
http://www.parashift.com/c++-faq-lit...html#faq-35.12

Best regards,

Tom
Thanks guys,

Unfortunately I still get an error. I have removed the SystemC stuff and
created a simple C++ example using the same template coding style as in the
FAQ, for some reason Visual C++ doesn't like the "simple template class"
solution, I did change the template<typename Tto template<int Tany idea?

// File "Foo.h"
template<int T>
class Foo {
public:
void g();
};

// File "Foo.cpp"
#include <iostream>
#include "Foo.h"
template<int T>
void Foo<T>::g()
{
std::cout << "T=" << T << std:endl;
}
template class Foo<int>; // Error C2975, without it LNK2019 Error

// File "main.cpp"
#include "Foo.h"
int main()
{
Foo<4x;
x.g();
}

Hans.
Jul 18 '06 #4

Hans wrote:
This is an FAQ:

http://www.parashift.com/c++-faq-lit...html#faq-35.12

Best regards,

Tom

Thanks guys,

Unfortunately I still get an error. I have removed the SystemC stuff and
created a simple C++ example using the same template coding style as in the
FAQ, for some reason Visual C++ doesn't like the "simple template class"
solution, I did change the template<typename Tto template<int Tany idea?

// File "Foo.h"
template<int T>
class Foo {
public:
void g();
};

// File "Foo.cpp"
#include <iostream>
#include "Foo.h"
template<int T>
void Foo<T>::g()
{
std::cout << "T=" << T << std:endl;
}
template class Foo<int>; // Error C2975, without it LNK2019 Error
Change the above line to:

template class Foo<4>;

and it should work fine. See below for explanation.
// File "main.cpp"
#include "Foo.h"
int main()
{
Foo<4x;
x.g();
}

Explanation: You can declare _either_ (a) template class Foo<int T>
_or_ (b) template class Foo<typename T>. Unfortunately, your code did
both - in one place defining it one way, the other place defining it
the other. My change above fixes that.

Best regards,

Tom

Jul 18 '06 #5
Hans wrote:
Thanks guys,

Unfortunately I still get an error. I have removed the SystemC stuff and
created a simple C++ example using the same template coding style as in the
FAQ, for some reason Visual C++ doesn't like the "simple template class"
solution, I did change the template<typename Tto template<int Tany idea?

// File "Foo.h"
template<int T>
class Foo {
public:
void g();
};

// File "Foo.cpp"
#include <iostream>
#include "Foo.h"
template<int T>
void Foo<T>::g()
{
std::cout << "T=" << T << std:endl;
You meant std::endl.
}
template class Foo<int>; // Error C2975, without it LNK2019 Error
This line is non-sense in context. You are instantiation Foo<with a
type rather than a constant int.
>
// File "main.cpp"
#include "Foo.h"
int main()
{
Foo<4x;
x.g();
}
The same comments about putting implementations for templates in the
header apply, and in fact, everything links fine if you fix the two
above errors and make sure all this code is visible in a single
translation unit.

Cheers! --M

Jul 18 '06 #6
mlimber wrote:
Hans wrote:
Thanks guys,

Unfortunately I still get an error. I have removed the SystemC stuff and
created a simple C++ example using the same template coding style as in the
FAQ, for some reason Visual C++ doesn't like the "simple template class"
solution, I did change the template<typename Tto template<int Tany idea?

// File "Foo.h"
template<int T>
class Foo {
public:
void g();
};

// File "Foo.cpp"
#include <iostream>
#include "Foo.h"
template<int T>
void Foo<T>::g()
{
std::cout << "T=" << T << std:endl;

You meant std::endl.
}
template class Foo<int>; // Error C2975, without it LNK2019 Error

This line is non-sense in context. You are instantiation Foo<with a
type rather than a constant int.
True.

// File "main.cpp"
#include "Foo.h"
int main()
{
Foo<4x;
x.g();
}

The same comments about putting implementations for templates in the
header apply, and in fact, everything links fine if you fix the two
above errors and make sure all this code is visible in a single
translation unit.
Actually, if one follows the explicit instantiation examples shown in
the FAQ, one does not need to put all the code in a single translation
unit. If the OP substitutes "template class Foo<4>;" for the line with
the error, it will work fine in separate translation units. I often
use the explicit instantiation trick to cut down on compilation times.

Best regards,

Tom

Jul 18 '06 #7
Thomas Tutone wrote:
mlimber wrote:
The same comments about putting implementations for templates in the
header apply, and in fact, everything links fine if you fix the two
above errors and make sure all this code is visible in a single
translation unit.

Actually, if one follows the explicit instantiation examples shown in
the FAQ, one does not need to put all the code in a single translation
unit. If the OP substitutes "template class Foo<4>;" for the line with
the error, it will work fine in separate translation units. I often
use the explicit instantiation trick to cut down on compilation times.
Right you are. I avoid that method, however, because it couples client
code to my template code, which is otherwise independent of it.

Cheers! --M

Jul 18 '06 #8
"mlimber" <ml*****@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
...
>template class Foo<int>; // Error C2975, without it LNK2019 Error

This line is non-sense in context. You are instantiation Foo<with a
type rather than a constant int.
>>
// File "main.cpp"
#include "Foo.h"
int main()
{
Foo<4x;
x.g();
}

The same comments about putting implementations for templates in the
header apply, and in fact, everything links fine if you fix the two
above errors and make sure all this code is visible in a single
translation unit.

Cheers! --M
Hi Guys,

Thanks for the explanation, however, can I draw one more time on your
collective expertise by asking how I could change "4" into a compile time
constant. What I mean by that is that I would like to instantiate Foo with
different constant values:

//File Foo.cpp
template class Foo<?>;

// File "main.cpp"
#include "Foo.h"
int main()
{
Foo<4x;
Foo<8y;
x.g();
}

Of course I could go with the FAQ-35.12 solution 1 which works fine but I am
pretty sure that this issue will come back to haunt me :-)

Thanks,
Hans.
Jul 19 '06 #9
Hans wrote:
Thanks for the explanation, however, can I draw one more time on your
collective expertise by asking how I could change "4" into a compile time
constant. What I mean by that is that I would like to instantiate Foo with
different constant values:

//File Foo.cpp
template class Foo<?>;
You have to include an explicit instantiation for every constant value
that you will need - that's the downside of this method. So here, you
would need to include the following two lines:

template class Foo<4>;
template class Foo<8>;
>
// File "main.cpp"
#include "Foo.h"
int main()
{
Foo<4x;
Foo<8y;
x.g();
}
The alternative is to follow mlimber's advice and put the full
definition of the template in the header file.

Best regards,

Tom

Jul 19 '06 #10

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

Similar topics

5
by: Ruthless | last post by:
hello. All XML and XSLT are processed by preprocessor as a trees. How can i simply display my XML as some kind of tree. given xml: <struct> <node level="1" no="1">
22
by: E. Robert Tisdale | last post by:
According to the C++ FAQ Lite: http://www.parashift.com/ What is "genericity"? Yet another way to say, "class templates." Not to be confused with "generality" (which just means avoiding...
11
by: Micha | last post by:
Hello there, I think I've run into some classic c++ pitfall and maybe some of you guys can help me out. For my project I will need to use matrices and vectors and so I decided to implement them...
2
by: SpotNet | last post by:
Hello Newsgroup, Made my own assembley that enables highly customisable Windows Common Dialogs, specifically FileOpen, FileSave, ChooseColor and ChooseFont. I'd done these before using VB 6.0....
0
by: Cat | last post by:
In the New Project window of Visual C# 2005 Express Edition Beta 2, there is a Search Online Templates option. When I double click it, it just opens VS Documentation Program. I coudln't find...
12
by: Mark P | last post by:
A couple template related questions: 1. In the code pasted below, why does the basic version get selected in the call to foo and the const version get selected in the call to bar? 2. When...
28
by: NewToCPP | last post by:
Hi, I am just trying to find out if there is any strong reason for not using Templates. When we use Templates it is going to replicate the code for different data types, thus increasing the...
104
by: JohnQ | last post by:
Well apparently not since one can step thru template code with a debugger. But if I was willing to make the concession on debugging, templates would be strictly a precompiler thing? I have a...
4
by: Alan Woodland | last post by:
I've been trying out more template metaprogramming ideas with typelists (mostly for personal learning, I'm aware boost most probably provides this facility already), and I've run into this small...
74
by: copx | last post by:
In "Learning Standard C++ as a New Language" Bjarne Stroustrup claims that properly written C++ outperforms C code. I will just copy his first example here, which is supposed to demonstrate how C++...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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...
0
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...
0
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...
0
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...

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.