468,513 Members | 927 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,513 developers. It's quick & easy.

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 4252
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by stef | last post: by
5 posts views Thread by xuatla | last post: by
6 posts views Thread by markww | last post: by
11 posts views Thread by asdf | last post: by
10 posts views Thread by Dennis Jones | last post: by
11 posts views Thread by Tim H | last post: by
27 posts views Thread by CodeMonk3y | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.