473,765 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(Addsu b)
{

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

SC_CTOR(Addsub) {
SC_METHOD(do_pr ocess123);
}
};

// File 2 addsub.cpp
template <int ADDSUB_WIDTH>
void Addsub<ADDSUB_W IDTH>::do_proce ss123(){}

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

#define WIDTH 24

SC_MODULE(Cordi c) {
public:

Addsub<WIDTHADD 1;

void do_process1() { }
SC_CTOR(Cordic) : ADD1("ADD1") {
SC_METHOD(do_pr ocess1);
}
};
Jul 18 '06 #1
9 1792
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(Addsu b)
{

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

SC_CTOR(Addsub) {
SC_METHOD(do_pr ocess123);
}
};

// File 2 addsub.cpp
template <int ADDSUB_WIDTH>
void Addsub<ADDSUB_W IDTH>::do_proce ss123(){}

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

#define WIDTH 24

SC_MODULE(Cordi c) {
public:

Addsub<WIDTHADD 1;

void do_process1() { }
SC_CTOR(Cordic) : ADD1("ADD1") {
SC_METHOD(do_pr ocess1);
}
};
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(Addsu b)
{

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

SC_CTOR(Addsub) {
SC_METHOD(do_pr ocess123);
}
};

// File 2 addsub.cpp
template <int ADDSUB_WIDTH>
void Addsub<ADDSUB_W IDTH>::do_proce ss123(){}

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

#define WIDTH 24

SC_MODULE(Cordi c) {
public:

Addsub<WIDTHADD 1;

void do_process1() { }
SC_CTOR(Cordic) : ADD1("ADD1") {
SC_METHOD(do_pr ocess1);
}
};
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<typena me 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<typena me 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<typena me 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<typena me 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.goo glegroups.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
2226
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
2191
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 solutions which are overly specific),
11
2603
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 by myself. I know there are already tons of vector and matrix implementations, but I wanted to have one taylored for my needs and without debugging someones else code. Also is's become somewhat personal meanwhile ;-).
2
1923
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. In order to place these dialogs on custom templates I need to construct the dialog templates (and compile as resources) using a utility that came with the C++ compiler called Dlgedit.exe. I want to create custom templates for my C# (2.0) dialogs...
0
1012
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 anything. Is there any website that I can download more Project Templates for VS C# 2005? Thanks!
12
1653
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 working with STL output iterators, is there any general advice on whether they should be passed by value or reference? I have templated code which takes ostream_iterators or back_insert_iterators which I inititally chose to pass by reference, but...
28
2637
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 executable size. This should not cause any performance issue. So, is it safe to say that if I can offered to have bigger image size I can go ahead and use Templates with out worrying about any other issues?
104
4609
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 feeling the answer I'm going to get back will be "no, because templates have taken on a life of their own since their original conception and now also affect compiler implementation" (read: not good, IMO. John
4
1903
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 problem here. Comeau online (without C++0x, in strict mode) accepts this example pasted here, (apologies for the length of it). Unfortunately G++ (3.4, 4.1, 4.2) doesn't, and complains about index_find being private. The exact error it gives is:...
74
4850
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++ abstractions do not only make code easier to understand but also make it more efficient: === The simplest specific example I can think of is a program to find the mean and median of a sequence of double precision floating-point numbers read...
0
9568
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9399
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9835
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8832
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
7379
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
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3924
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
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
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.