0

Is there any way to use a template parameter as a variable? for example if I have a function

template<int dim>
void DomainGrid<dim>::getData(Data data_, int field_dim)
{
    int size_ = field_dim *dim; // Compiler Error Here
    for(int i =0; i<size_; ++i)
       std::cout<<data_[i]<<std::endl;
}

Can I get a similar functionality? Compiling this function produces an error at

invalid use of member (did you forget the ‘&’ ?)

the error appears regardless of whether the function is called or not

Which I guess is because of this problem...

Community
  • 1
  • 1
  • 3
    What's the definition of `DomainGrid`? Chances are you shouldn't be saying `` twice. – Kerrek SB Jun 25 '11 at 12:46
  • thanks for your replies..... yes you are right... but in a different context there is a function field_dim(). I actually wanted to use a member field_dim_. The mistake just escaped my eyes. –  Jun 25 '11 at 13:29

2 Answers2

2
int size_ = field_dim *dim; // Compiler Error Here

This is perfectly fine. This cannot be an error.

The real problem is not in the code which you've posted here. So post the definition of DomainGrid and the complete error messages.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • 3
    @Als: Based on the post and the information given, I think its an answer. It tells him to look for the real problem which is not in the code posted. – Nawaz Jun 25 '11 at 12:53
  • @Als, we have a downvote button if you don't think the answer is useful. – Michael Kristofik Jun 25 '11 at 13:03
  • @Als: Well, maybe you should use it more often then. ;) The downvote button is there for a reason. ;) – jalf Jun 25 '11 at 13:35
  • I am deleting some of my comments because certain modifications were made to the answer later on, which make my comments feel unwarranted. – Alok Save Jun 25 '11 at 14:02
  • @Als: If the second sentence which starts with *`The real problem is not...`* makes your comment unwarranted, then it was unwarranted before as well, because there is nothing in that sentence which adds anything new, except that its a repetition of the first sentence, as the first sentence pretty much says that the problem isn't in that line which OP thinks, causes the error. – Nawaz Jun 25 '11 at 14:08
2

Rename either the template parameter or the member function?

That seems like the simplest, most straightforward solution. Whenever multiple symbols have the same name, you risk name clashes. So don't use the same name for multiple symbols in such cases.

(This is assuming you have a member function dim(). I assume so based on the error message, but it might have been useful information to actually include in the question.)

jalf
  • 243,077
  • 51
  • 345
  • 550