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

Initialization of data members


Hello all,

In the code below, why does a syntax error appear at the location commented
"Syntax error!!!"? What language rule is coming into play here?

The example below also shows an alternative (use of an initializer list) to
the illegal syntax I attempted to use, but I'm still left wondering why I
could not do what I attempted...

Thanks,
Dave

#include <iostream>
#include <vector>

using namespace std;

class foo
{
public:
foo(): a(10) {for (int i = 0; i != a.size(); ++i) cout << a[i] <<
endl;}

private:
vector<int> a;
vector<int> b(10); // Syntax error!!!
};

int main()
{
foo y;

return 0;
}

Jul 19 '05 #1
3 1718

"Dave Theese" <ch**********@yahoo.com> wrote in message news:vm************@news.supernews.com...
In the code below, why does a syntax error appear at the location commented
"Syntax error!!!"? What language rule is coming into play here?

You can not intialize non-static members in their declaration in the class.
That has to be done with the constructor. You obviously have the
clue. The member "a" is done the right way.
class foo
{
public:
foo(): a(10) {for (int i = 0; i != a.size(); ++i) cout << a[i] <<
endl;}

private:
vector<int> a;
vector<int> b(10); // Syntax error!!!
};

Jul 19 '05 #2
Dave Theese wrote:
Hello all,

In the code below, why does a syntax error appear at the location
commented "Syntax error!!!"? What language rule is coming into play
here?

The example below also shows an alternative (use of an initializer
list) to the illegal syntax I attempted to use, but I'm still left
wondering why I could not do what I attempted...

Thanks,
Dave

#include <iostream>
#include <vector>

using namespace std;

class foo
{
public:
foo(): a(10) {for (int i = 0; i != a.size(); ++i) cout << a[i]
<< endl;}

private:
vector<int> a;
vector<int> b(10); // Syntax error!!!


These are member variable _declarations_, not definitions. If you give an
initializer it becomes a definition: the compiler understand it as "I want
to create variable here". But there is no here, you are in the middle of a
class definition.

I am sure the standard has some nice words about it, much more complicated
ones then I wrote. it may not even be exactly as I have said: but this is
the basic idea. Class member variables have to be initialized by a
constructor of that class.

This can be frustrating, if you have 100 constructors and you have to write
this b(10) into each of them. AFAIK there is a proposal (forming?) to add a
possibility to "chain constructors", so that you only need to write such
things once.

--
WW aka Attila
Jul 19 '05 #3
Dave Theese wrote:
Hello all,

In the code below, why does a syntax error appear at the location commented
"Syntax error!!!"? What language rule is coming into play here?


members need to be initialized in the constructor.
One thing you could use is a helper template class - it will only work
for parameters that can be made template parameters. Here is an example
for a simple one parameter version of the template.

BTW - any critique is welcome on this template. I'm about to "finish it
up" and release the puppy in my GPL'd C++ library.
template < typename w_type, typename w_init_type, w_init_type w_val >
class AT_IType
{
public:

w_type m_value;

inline AT_IType()
: m_value( w_val )
{
}

inline operator w_type & ()
{
return m_value;
}

inline operator const w_type & () const
{
return m_value;
}

inline w_type & operator = ( const w_type & i_value )
{
m_value = i_value;

return m_value;
}

};

#include <vector>

class Foo
{
public:
AT_IType< std::vector<int>, int, 10 > x;

};

Jul 19 '05 #4

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

Similar topics

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
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...
10
by: JKop | last post by:
What's the difference between them? Take the following: #include <iostream> struct Blah { int k;
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...
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....
3
by: Steve Folly | last post by:
Hi, I had a problem in my code recently which turned out to be the 'the "static initialization order fiasco"' problem (<http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12>) The FAQ...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
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...
4
by: subramanian100in | last post by:
Suppose I have #include <cstdlib> #include <iostream> using namespace std; class Test { public:
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
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?
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...

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.