compile error about member variable initialization

G

George2

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'
--------------------

Code:
#include <vector>

using namespace std;

class Foo {

private:

	vector<int>& vi;

public:

	Foo (vector<int>& vi_in)
	{
		vi = vi_in;
	}
/*
	Foo (vector<int>& vi_in) : vi (vi_in)
	{
	}
*/
};

int main()
{
	vector<int> vi;
	Foo foo = Foo (vi);


	return 0;
}


thanks in advance,
George
 
V

Victor Bazarov

George2 said:
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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,965
Messages
2,570,148
Members
46,710
Latest member
FredricRen

Latest Threads

Top