0

I have run into this situation where I need to constantly pass a vector around. The ideal idea is to pass it by reference so the program doesn't slow down from constantly copying the vector.

class Bar has the original vector.

Bar.h:

#ifndef Bar_h
#define Bar_h

#include <iostream>
#include <vector>

using namespace std;

class Bar{

public:

    Bar();
    vector<int> & getVec();

private:
    vector<int> vec;
};
#endif

Bar.cpp:

#include <iostream>
#include "Bar.h"

using namespace std;

Bar::Bar(){
    vec.push_back(1);
    vec.push_back(2);
    vec.push_back(3);
    vec.push_back(4);
    vec.push_back(5);
}

vector<int> & Bar::getVec(){
    return vec;
}

Class foo receives the vector.

foo.h:

#ifndef Foo_h
#define Foo_h

#include <iostream>
#include <vector>

using namespace std;

class Foo{

public:

    Foo();

private:
    vector<int> & vecRef;
};
#endif

foo.cpp:

#include <iostream>
#include "Foo.h"
#include "Bar.h"

using namespace std;

Foo::Foo(){

    Bar bar;



    vecRef = bar.getVec();
}

main:

#include <iostream>
#include "Foo.h"

using namespace std;

void main(){

    Foo foo();

}

The problem is that when compiling I get Error code C2758 which stats "a member of reference type must be initialized". It is referring to the foo class and vector<'int> & vecRef; not being initialized properly. My question is how would I go about properly initializing vecRef while keeping its declaration in foo.h? (i have done it successfully by declaring vecRef in foo.cpp, but that's not what i want if possible). OR is adding the '&' into vector<'int> & vecRef; not the way to go about this at all?

1
  • Foo(); constructor must initialize the reference vecRef in a member initializer list. It's not possible without Bar passed as a constructor parameter or being a member variable of Foo. Commented Jul 29, 2015 at 15:31

2 Answers 2

0

References must be initialized in the member initializer list here:

Foo::Foo()
: vecRef(???) // <==
{ }

If a member variable isn't listed there, it will be default-initialized, and a reference cannot be default-initialized - hence the error. Furthermore, it's a good thing your code doesn't compile, because otherwise:

{
    Bar bar;
    vecRef = bar.getVec();
} // <== bar gets destroyed here
  // vecRef is now a dangling reference

What you likely want to do is pass a Bar into Foo's constructor, by reference, and then initialize vecRef like so:

Foo::Foo(Bar& bar)
: vecRef(bar.getVec())
{ }

Or just take the reference directly:

Foo::Foo(std::vector<int>& v)
: vecRef(v)
{ }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your reply. I wasn't able to get your first suggestion working for what ever reason (I'm new to C++) but the 2nd one worked fine. Ill try to incorporate this method into my real program now. Thanks!
0

A reference cannot reference nothing. Thus you have to initialize foos vecRef in the constructor. Actually this is explained already in detail in Barrys answer and I just wanted to point out the following:

Once you provide public access to a reference of a member variable, there is no point in making this member variable private. You could as well make vec itself public in Bar (and not need a getter method). It would have the same effect but with less superfluous typing needed from you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.