By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,959 Members | 1,136 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,959 IT Pros & Developers. It's quick & easy.

Initialization lists for nested class members.

P: 51
Hi all,
I couldnt initialize the nested class members using constructor initialization list's. I am using Intel/Solaris/g++ compiler.

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include<iostream>
  3.  
  4. template<class T>
  5. class A{
  6. private:
  7.   template<class U>
  8.   struct B{
  9.     U a;
  10.     B():a(23){std::cout<<a; }
  11.     B(U e):a(e){std::cout<<a;}
  12.   };
  13. public:
  14.   //B<int> k     -----------> works  fine and prints out 23.
  15.   B<int> k(23); ----------->This doesn't work.
  16. };
  17.  
  18.  
  19. int main(){
  20.      A<int> a;
  21. }
  22.  
  23.  
Errors:

st.cpp:13: error: expected identifier before numeric constant
st.cpp:13: error: expected ',' or '...' before numeric constant.




Thanks in advance,
gs.
Aug 13 '07 #1
Share this Question
Share on Google+
4 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
B<int> k just says that struct B sopecialized with int is a prioovate member of A.

That's OK.

But this:

B<int> k(23);

says to create an object K from B<int> and call the constructor with the int argunment. However, this is a template definition. No object is being created. Hence, you can't make function calls like this.

Always remember that the compiler makes a copy of the template and plugs in the vaues for the type in the copy. It is the copy that compiles. The remplate itself never compiles.

The trick here is to write a class using specific types and get that to compile. Then convert the class to a template.
Aug 13 '07 #2

P: 51
gsi
Hi,
Thanks for your reply. But I have some queries.

pls advice if I am wrong , you mean to say that , by saying B<int >k inside the class definition it involves an implicit function call to the default constructor of the struct specialization which the compiler itself takes care(during the object allocation), But by saying B<int>k(23) the user forces a function call to the overloaded constructor which he is not allowed to do(bfore the allocation).

Also I couldnt figure out what this mean's "The trick here is to write a class using specific types and get that to compile. Then convert the class to a template".
I tried to googling for the terms but couldnt make out anything, Pls give me a bit more explanation.

Thanks in advance,
gsi.
Aug 14 '07 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
you mean to say that , by saying B<int >k inside the class definition it involves an implicit function call to the default constructor of the struct specialization which the compiler itself takes care(during the object allocation), But by saying B<int>k(23) the user forces a function call to the overloaded constructor which he is not allowed to do(bfore the allocation).
Forget about the template part. Here is your code specialized with int:
Expand|Select|Wrap|Line Numbers
  1. class A{
  2. private:
  3.  
  4.   struct B{
  5.     int a;
  6.     B():a(23){std::cout<<a; }
  7.     B(int e):a(e){std::cout<<a;}
  8.   };
  9. public:
  10.   //B<int> k     -----------> works  fine and prints out 23.
  11.   B k(23); //----------->This doesn't work.
  12. };
  13.  
  14.  
  15. int main(){
  16.      A  a;
  17. }
  18.  
It still won't compile. It's the line B k(23) and the compiler is not expecting a constant. That is, you can't say in a class definition what constructor to call. That's because a class is a declaration (allocates no memory) but B k(23) is a definition (k must be created in order to process the 23). You cannot have function calls inside a class definition.

B<int> k is really:
Expand|Select|Wrap|Line Numbers
  1. struct B{
  2.     int a;
  3.     B():a(23){std::cout<<a; }
  4.     B(int e):a(e){std::cout<<a;}
  5.  }k;
  6.  
There's no object yet. Just a copy of the template.

Also I couldnt figure out what this mean's "The trick here is to write a class using specific types and get that to compile. Then convert the class to a template".
Suppose you need a function template to sum the elements of an array. You could just write the template, then specialize it, then debug it. However, it's often easier to write a regular function:

Expand|Select|Wrap|Line Numbers
  1. int Sum(int* arr, int len)
  2. {
  3.     int total = 0;
  4.     for (int i = 0; i < len; ++i)
  5.     {
  6.          total += arr[i]]
  7.     }
  8.     return total;
  9. }
  10.  
Then debug it. When it works, change the function into a template:
Expand|Select|Wrap|Line Numbers
  1. template<typename T>
  2. T Sum(T* arr, int len)
  3. {
  4.     T total = 0;
  5.     for (int i = 0; i < len; ++i)
  6.     {
  7.          total += arr[i]]
  8.     }
  9.     return total;
  10. }
  11.  
Aug 14 '07 #4

P: 51
gsi
Hi,
Thanks a lot for the explanation...

Yes i do realize now that function call's are illegal from a class definition since class definition is merely a type constructor.

thanks,
gsi.
Aug 14 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.