473,375 Members | 1,669 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,375 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>
  3. using namespace std;
  5. class Foo {
  7. private:
  9. vector<int>& vi;
  11. public:
  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. };
  24. int main()
  25. {
  26. vector<intvi;
  27. Foo foo = Foo (vi);
  30. return 0;
  31. }

thanks in advance,
Dec 4 '07 #1
1 4479
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.

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

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...
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 {...
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='...
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...
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){}
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...
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*...
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...
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 --
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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 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.