473,396 Members | 1,760 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,396 software developers and data experts.

compile error about member variable initialization

Hello everyone,
Why the below code segment will result in compile error? When I change
code to the comment one (constructor), it can compile. The compiler is
too stupid? :-)

I am using Visual Studio 2005.

--------------------
main.cpp(13) : error C2758: 'Foo::vi' : must be initialized in
constructor base/member initializer list

see declaration of 'Foo::vi'
--------------------

Expand|Select|Wrap|Line Numbers
  1. #include <vector>
  2.  
  3. using namespace std;
  4.  
  5. class Foo {
  6.  
  7. private:
  8.  
  9. vector<int>& vi;
  10.  
  11. public:
  12.  
  13. Foo (vector<int>& vi_in)
  14. {
  15. vi = vi_in;
  16. }
  17. /*
  18. Foo (vector<int>& vi_in) : vi (vi_in)
  19. {
  20. }
  21. */
  22. };
  23.  
  24. int main()
  25. {
  26. vector<intvi;
  27. Foo foo = Foo (vi);
  28.  
  29.  
  30. return 0;
  31. }
  32.  

thanks in advance,
George
Dec 4 '07 #1
1 4481
George2 wrote:
Why the below code segment will result in compile error? When I change
code to the comment one (constructor), it can compile. The compiler is
too stupid? :-)

I am using Visual Studio 2005.

--------------------
main.cpp(13) : error C2758: 'Foo::vi' : must be initialized in
constructor base/member initializer list
[..]
No, the compiler is not too stupid. References have to be initialised.
The only place where reference members can be initialised is the c-tor
initialiser list.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 4 '07 #2

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

Similar topics

6
by: stef | last post by:
Anyone have any guesses as to why I am getting an error message when I try to compile: In file included from CP.h:13,from driver.cpp:5: cb.h:33: invalid data member initialization cb.h:33: (use...
10
by: Fred Ma | last post by:
Are there any reasons that would make it bad for C++ to allow simultaneous declaration and initilization of member data? Current way: ------------ class DerivedClass : BaseClass { { enum {...
5
by: xuatla | last post by:
Hi, I encountered the following compile error of c++ and hope to get your help. test2.cpp: In member function `CTest CTest::operator+=(CTest&)': test2.cpp:79: error: no match for 'operator='...
6
by: markww | last post by:
Hi, I put a static member variable in my class. class CMine { static int m_nCount; }; How do I initialize it to zero? I can't do that in the constructor of the class can I? Won't that...
11
by: asdf | last post by:
The oder of member initialization is the order in which the members are defined. So the following code is problematic: class X{ int i; int j; public: X(int val):j(val),i(j){}
10
by: Dennis Jones | last post by:
Hello, I have a hierarchy of classes in which there will be a data element that is common to all descendant classes. This element (a string in this case) is constant, but specific to each...
15
by: Bob Johnson | last post by:
I have a base class that must have a member variable populated by, and only by, derived classes. It appears that if I declare the variable as "internal protected" then the base class *can*...
11
by: Tim H | last post by:
The following program compiles just fine in C, but not in C++. Can anyone explain why? I have a chunk of code that defines stuff like this in headers (without the extern) that I can not easily...
27
by: CodeMonk3y | last post by:
gotta question on sizeof keyword does the sizeof keyword calcuates the size at compile time or run time ?? -- Posted on news://freenews.netfront.net - Complaints to news@netfront.net --
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
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...

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.