简体   繁体   English

如何正确初始化向量 <int> &vecRef? 抛出错误C2758

[英]How to properly Initialize vector<int> & vecRef? Throwing error C2758

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类具有原始矢量。

Bar.h: 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: 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类接收该向量。

foo.h: 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: 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". 问题是在编译时出现错误代码C2758,其状态为“必须初始化引用类型的成员”。 It is referring to the foo class and vector<'int> & vecRef; 它指的是foo类和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? 我的问题是我如何正确地初始化vecRef,同时将其声明保留在foo.h中? (i have done it successfully by declaring vecRef in foo.cpp, but that's not what i want if possible). (我已经通过在foo.cpp中声明vecRef成功完成了此操作,但是如果可能的话,那不是我想要的)。 OR is adding the '&' into vector<'int> & vecRef; 或将'&'添加到vector <'int>&vecRef;中; not the way to go about this at all? 根本没有办法解决吗?

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: 您可能想做的是通过引用将Bar传递给Foo的构造函数,然后像这样初始化vecRef

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

Or just take the reference directly: 或者直接参考:

Foo::Foo(std::vector<int>& v)
: vecRef(v)
{ }

A reference cannot reference nothing. 引用不能引用任何内容。 Thus you have to initialize foo s vecRef in the constructor. 因此,您必须在构造函数中初始化foovecRef Actually this is explained already in detail in Barrys answer and I just wanted to point out the following: 实际上,这已经在Barrys答案中进行了详细说明,我只想指出以下几点:

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). 您也可以在Bar公开vec本身(不需要getter方法)。 It would have the same effect but with less superfluous typing needed from you. 它将具有相同的效果,但是您需要的多余键入更少。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的第三方通话出现C2758错误 - got C2758 error for my third party call 如何初始化向量数组 <int> 在C ++中具有预定义的计数? - How to initialize an array of vector<int> in C++ with predefined counts? 如何在C ++中初始化const int二维向量 - How to initialize a const int 2-dimension vector in C++ 如何初始化配对 <vector<pair<bool,int> &gt;,向量 <pair<bool,int> &gt;&gt;在C ++ 11中 - How to initialize pair<vector<pair<bool,int>>,vector<pair<bool,int>>> in C++11 如何从std :: vector &lt;&gt;正确初始化向量 - How to properly initialize a vector from std::vector<> 如何初始化并填充vector类型的vector <vector<int> &gt;? - How do I initialize and fill a vector of type vector<vector<int> >? 如何将输入正确读入二维向量,向量<vector<int> &gt; 在 c++ </vector<int> - how to read input properly into a 2D vector, vector<vector<int>> in c++ 如何正确转换向量 <int> 到一个void *并返回到一个向量 <int> ? - How to properly convert a vector<int> to a void* and back to a vector<int>? 如何在C ++中声明和初始化2d int向量? - How do I declare and initialize a 2d int vector in C++? 如何初始化向量 <int> v(1000)的值为{1,1,1,…,1},在C ++中没有循环? - How to initialize a vector<int> v(1000) with values {1,1,1,…,1} without a loop in C++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM