473,406 Members | 2,745 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,406 software developers and data experts.

Initialization lists for nested class members.

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
4 2287
weaknessforcats
9,208 Expert Mod 8TB
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
gsi
51
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
9,208 Expert Mod 8TB
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
gsi
51
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

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

Similar topics

8
by: CoolPint | last post by:
I read in books that nested class cannot access private members of nesting class and vice versa unless they are made friends. Somehow, my compiler is letting my nested class member functions access...
50
by: Charles Stapleton | last post by:
Given the folowing class class Ctest{ public: Ctest( int i, int j) :a(i) { b = j; } private: int a, b; } When creating an object of type Ctest, what advantage is there to setting
1
by: Jacek Dziedzic | last post by:
Hi! A) Why isn't it possible to set a member of the BASE class in an initialization list of a DERIVED class constructor (except for 'calling' the base constructor from there, of course)? I even...
3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
8
by: Jef Driesen | last post by:
Hello, Suppose I have a class that looks like this: class point { protected: unsigned int m_x, m_y; // OR unsigned int m_data; public: point(); point(unsigned int x, unsigned int y);
4
by: Jacek Dziedzic | last post by:
Hello! Suppose I have a class Foo that defines a default c'tor that initializes some data using an initialization list: Foo::Foo() : member1(0), member2(0), member3(NULL), member4(20) // and...
1
by: pasa_1 | last post by:
Can someone clarify few items from FAQ http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6 ' Should my constructors use "initialization lists" or "assignment"?' a. This might happen...
8
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it....
8
by: Sheldon | last post by:
Hi, Can anyone help with this problem with setting up nested structures and initializing them for use. I have created several structs and placed them in a super struct that I will then pass to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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,...
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.