473,382 Members | 1,431 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,382 software developers and data experts.

Invalid template argument

Hello all,

Okay, I've got a templated class that that takes an int and a char *, but
when I try to instantiate an object of that template, VS.NET complains with:

error C2975: 'S' : invalid template argument for 'TextDB::TextDB_Version',
constant expression expected

template<int N, char *S>
class TextDB_Version
{
[...]
};

#define TDB_DEFAULT_DELIMITER "."
class TextDB
{
private:
TextDB_Version<3, TDB_DEFAULT_DELIMITER> myVersion;
};

I can't understand what's wrong with this. It seems to me from
documentation that I should be able to use char *'s as literal template
arguments, what am I missing here? I tried taking out the #define and put
in just a string there, TextDB_Version<3, "."> myVersion; and that had the
same error obviously.

Thanks for everyone's time,
Kevin Grigorenko
Jul 19 '05 #1
8 10623
On Mon, 15 Sep 2003 12:57:44 -0400, "Kevin Grigorenko"
<kz****@psu.edu> wrote:
Hello all,

Okay, I've got a templated class that that takes an int and a char *, but
when I try to instantiate an object of that template, VS.NET complains with:

error C2975: 'S' : invalid template argument for 'TextDB::TextDB_Version',
constant expression expected

template<int N, char *S>
class TextDB_Version
{
[...]
};

#define TDB_DEFAULT_DELIMITER "."
class TextDB
{
private:
TextDB_Version<3, TDB_DEFAULT_DELIMITER> myVersion;
};

I can't understand what's wrong with this. It seems to me from
documentation that I should be able to use char *'s as literal template
arguments, what am I missing here? I tried taking out the #define and put
in just a string there, TextDB_Version<3, "."> myVersion; and that had the
same error obviously.


Literals don't have external linkage, but template parameters must. So
you need:

char* TDB_DEFAULT_DELIMITER = "."; //has external linkage.

class TextDB
{
private:
TextDB_Version<3, TDB_DEFAULT_DELIMITER> myVersion;
};

Tom
Jul 19 '05 #2
tom_usenet wrote:
error C2975: 'S' : invalid template argument for
'TextDB::TextDB_Version', constant expression expected
[SNIP] Literals don't have external linkage, but template parameters must. So
you need:

char* TDB_DEFAULT_DELIMITER = "."; //has external linkage.


Is that really a compile time constant (required for template
instantiation)?

--
WW aka Attila
Jul 19 '05 #3
tom_usenet wrote:
On Mon, 15 Sep 2003 12:57:44 -0400, "Kevin Grigorenko"
<kz****@psu.edu> wrote:
.....

template<int N, char *S>
class TextDB_Version
{

....

Literals don't have external linkage, but template parameters must. So
you need:

char* TDB_DEFAULT_DELIMITER = "."; //has external linkage.

class TextDB
{
private:
TextDB_Version<3, TDB_DEFAULT_DELIMITER> myVersion;
};

Tom


The alternative is to use a single char as a delimiter:

template<int N, char D>

like:

TextDB_Version<3, '.'>

if you only have single character delimiters.

Jul 19 '05 #4
"Kevin Grigorenko" <kz****@psu.edu> wrote in message
news:bk***********@f04n12.cac.psu.edu...
Hello all,

SNIP

I can't understand what's wrong with this. It seems to me from
documentation that I should be able to use char *'s as literal template
arguments, what am I missing here? I tried taking out the #define and put
in just a string there, TextDB_Version<3, "."> myVersion; and that had the
same error obviously.


This is addressed many times in the archives (but not in the FAQ yet, it
appears). I'm sure you can search google groups for the long answer. The
short answer is char* is fine for template arguments, but you just can't
create templates of the form TemplateClass<"text">. In this particular
case, why don't you just declare your template as:

template<int N, char S>
class TextDB_Version
{
[...]
};

and then you can instantiate with

TextDB_Version<3, '.'>
Jul 19 '05 #5
"Kevin Saff" <go********@kevin.saff.net> wrote in message
news:HL********@news.boeing.com...
"Kevin Grigorenko" <kz****@psu.edu> wrote in message
news:bk***********@f04n12.cac.psu.edu...
Hello all,

SNIP

I can't understand what's wrong with this. It seems to me from
documentation that I should be able to use char *'s as literal template
arguments, what am I missing here? I tried taking out the #define and put in just a string there, TextDB_Version<3, "."> myVersion; and that had the same error obviously.


This is addressed many times in the archives (but not in the FAQ yet, it
appears). I'm sure you can search google groups for the long answer. The
short answer is char* is fine for template arguments, but you just can't
create templates of the form TemplateClass<"text">. In this particular
case, why don't you just declare your template as:

template<int N, char S>
class TextDB_Version
{
[...]
};

and then you can instantiate with

TextDB_Version<3, '.'>


I'm sorry about being a little bit oblivious, but where are the archives? I
will use this suggestion however and just use one character.

The interesting thing is that in the description of the error, VS.NET gives
this as an example of how to fix it:

// C2975.cpp
template <char *P>
class x
{
char * f()
{
return P;
}
};

x<"abc"> *p = 0; // C2975 addr of object with internal linkage
Is this standardized? Does it have something to do with "internal linkage,"
which i must say i'm also oblivious about.

Thanks again,
Kevin Grigorenko
Jul 19 '05 #6
Kevin Grigorenko wrote:

I'm sorry about being a little bit oblivious, but where are the archives?


http://groups.google.com

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #7

"Kevin Grigorenko" <kz****@psu.edu> wrote in message
news:bk***********@f04n12.cac.psu.edu...
I'm sorry about being a little bit oblivious, but where are the archives? I will use this suggestion however and just use one character.
Sorry, here's a link:
http://groups.google.com/groups?q=comp.lang.c%2B%2B

and here are some posts you may be interested in:
http://groups.google.com/groups?hl=e...oup%3Dcomp.lan
g.c%252B%252B.*


The interesting thing is that in the description of the error, VS.NET gives this as an example of how to fix it:

> // C2975.cpp
template <char *P>
class x
{
char * f()
{
return P;
}
};

x<"abc"> *p = 0; // C2975 addr of object with internal linkage

Is this standardized? Does it have something to do with "internal linkage," which i must say i'm also oblivious about.


That looks like just an example of the error, rather than a fix. They're
constructing a null pointer to a x<"abc"> object, but "abc" has internal
linkage so isn't eligible for use as a template parameter.

Internal linkage means the variable name can't be accessed in external
compilation units. For one thing, note that x<"abc"> would at least
represent a different class in a different compilation unit, because the
address of the string will be different. This means at best the template
class would have internal linkage as well. Still it seems like there could
be use for such a thing; apparently the full reason template parameters
cannot have internal linkage has something to do with how template names are
required to be mangled.

Jul 19 '05 #8
On Mon, 15 Sep 2003 20:50:24 +0300, "White Wolf" <wo***@freemail.hu>
wrote:
tom_usenet wrote:
error C2975: 'S' : invalid template argument for
'TextDB::TextDB_Version', constant expression expected

[SNIP]
Literals don't have external linkage, but template parameters must. So
you need:

char* TDB_DEFAULT_DELIMITER = "."; //has external linkage.


Is that really a compile time constant (required for template
instantiation)?


Whoops, that was meant to be:

char TDB_DEFAULT_DELIMITER[] = "."; //has external linkage.

Tom
Jul 19 '05 #9

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

Similar topics

2
by: Elven | last post by:
Hi! I was trying to find the solution to this problem, but I don't think I could quite come up with the correct keywords to find it, since I'm pretty sure it's been asked before. In short,...
10
by: Matthias | last post by:
Hello, I thought one major advantage of using functors as e.g. sorting predicates over functions would be that I can do something like this: void foo() { class Predicate { public:
3
by: Chris | last post by:
I am having a very strange problem involving virtual functions in template classes. First of all, here is an extremely simplified structure of the two classes I am having problems with. ...
1
by: Timbo | last post by:
Hi all, This is my first message here so i'll try and include all the information that will help you help me out, if possible. Basically I am using C# in ASP.NET 2.0 and have a Repeater...
1
by: Java Guy | last post by:
I'm trying to view a web page. IE tells me there are (Java?) errors on the page. Here they are: Line: 15 Char: 7 Error: Wrong number of arguments or invalid propert assignment Code: 0 URL:...
4
by: robert | last post by:
On a server the binary (red hat) installed python2.4 and also a fresh compiled python2.5 spits "sem_post: Invalid argument". What is this and how can this solved? Robert ============== ...
2
by: Barry | last post by:
The problem brought by one earlier post from comp.lang.c++ http://groups.google.com/group/comp.lang.c++/browse_thread/thread/bf636c48b84957b/ I take part of the question and reproduce the code to...
8
by: William Xu | last post by:
Compiling: template <class T = int> T foo(const T& t) {} int main(int argc, char *argv) {} gcc complains:
2
by: Clyde | last post by:
Hi, what i'm trying to do is: /////////////// Code Start template <class TType, int* p = 0> class Template { public:
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.