Hi all,
I couldnt initialize the nested class members using constructor initialization list's. I am using Intel/Solaris/g++ compiler. -
-
#include<iostream>
-
-
template<class T>
-
class A{
-
private:
-
template<class U>
-
struct B{
-
U a;
-
B():a(23){std::cout<<a; }
-
B(U e):a(e){std::cout<<a;}
-
};
-
public:
-
//B<int> k -----------> works fine and prints out 23.
-
B<int> k(23); ----------->This doesn't work.
-
};
-
-
-
int main(){
-
A<int> a;
-
}
-
-
Errors:
st.cpp:13: error: expected identifier before numeric constant
st.cpp:13: error: expected ',' or '...' before numeric constant.
Thanks in advance,
gs.
4 2287
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.
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.
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: -
class A{
-
private:
-
-
struct B{
-
int a;
-
B():a(23){std::cout<<a; }
-
B(int e):a(e){std::cout<<a;}
-
};
-
public:
-
//B<int> k -----------> works fine and prints out 23.
-
B k(23); //----------->This doesn't work.
-
};
-
-
-
int main(){
-
A a;
-
}
-
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: -
struct B{
-
int a;
-
B():a(23){std::cout<<a; }
-
B(int e):a(e){std::cout<<a;}
-
}k;
-
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: -
int Sum(int* arr, int len)
-
{
-
int total = 0;
-
for (int i = 0; i < len; ++i)
-
{
-
total += arr[i]]
-
}
-
return total;
-
}
-
Then debug it. When it works, change the function into a template: -
template<typename T>
-
T Sum(T* arr, int len)
-
{
-
T total = 0;
-
for (int i = 0; i < len; ++i)
-
{
-
total += arr[i]]
-
}
-
return total;
-
}
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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
|
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...
|
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...
|
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);
|
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...
|
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...
|
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....
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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: 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...
|
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,...
|
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...
|
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,...
|
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...
| |