【问题标题】:Error while assigning a vector of object in constructor in c++在 C++ 的构造函数中分配对象向量时出错
【发布时间】:2020-05-11 18:28:37
【问题描述】:

我有这个代码:

#ifndef APPSYSTEM_H
#define APPSYSTEM_H
#include "Application.h"

#include <iostream>
#include <exception>
#include <vector> 

using namespace std;

class AppSystem{
private:
    vector<Application &> &ApplicationVector;
public:
    AppSystem(vector<Application &> &); //AppSystem Constructor
    AppSystem(const AppSystem &); //Copy constructor
    void setApplicationVector(vector<Application &> &); //set the AppSystem's Application Vector
    vector<Application &> getApplicationVector(); //get the AppSystem's Application Vector        
    virtual ~AppSystem(); //Destructor
};

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

//AppSystem Constructor
AppSystem::AppSystem(vector<Application &> &appSystemVector){
    ApplicationVector = appSystemVector;
} 

//Copy constructor 
AppSystem::AppSystem(const AppSystem &appSystem){
    ApplicationVector = appSystem.ApplicationVector;
}

//set the AppSystem's Application Vector
void AppSystem::setApplicationVector(vector<Application &> &applicationVector){
    this->ApplicationVector = applicationVector;
}

//get the AppSystem's Application Vector       
vector<Application &> AppSystem::getApplicationVector(){
    return this->ApplicationVector;
}

//Destructor
AppSystem::~AppSystem(){
    cout << "Destroying AppSystem Object " << endl; 
}

#include "AppSystem.h"
#include "Application.h"
#include "ApplicationConstructor.h"
#include "UserOpinion.h"
#include "MyExceptions.h"
#include <iostream>
#include <cstdlib>
#include <vector>

using namespace std;

int main(int argc, char** argv) {

    ApplicationConstructor appConstructor1("3324","Konstantinos Dimos", "konstantinos@uniwa.gr");
    ApplicationConstructor appConstructor2("3332","Nikos Paulou", "nikos@uniwa.gr");
    ApplicationConstructor appConstructor3("4432","Stavros", "stavros@uniwa.gr");
    Application app1("game1456", "Tetris", "1.0.0", appConstructor1, NULL, 10.0);    
    Application app2("game2245", "Pacman", "1.1.0", appConstructor2, NULL, 15.0);    
    Application app3("game1433", "Doom",  "1.1.1", appConstructor3, NULL, 20.0);
    /*-----------------------------------------------------------------------------------------------------*/
    ApplicationConstructor appConstructor4("2232","Eirini Markou", "eirini@uniwa.gr");
    ApplicationConstructor appConstructor5("1121","Tasos Sotiriou", "tasos@uniwa.gr");
    ApplicationConstructor appConstructor6("4431","Giorgos Papadopoulos", "giorgos@uniwa.gr");
    Application app4("desk4552", "Office", "1.0.0", appConstructor4, NULL, 30.0);    
    Application app5("desk6657", "Photoshop", "1.1.0", appConstructor5, NULL, 25.0);    
    Application app6("desk6643", "Torrent",  "1.1.1", appConstructor6, NULL, 45.0);
    /*-----------------------------------------------------------------------------------------------------*/
    Application appTable[] = {app1, app2, app3, app4, app5, app6};
    vector<Application &> appVector(appTable);   
    AppSystem appSystem(appVector);
}

我得到这个错误:

AppSystem.cpp:12:1:错误:'class std::vector&'中未初始化的引用成员 [-fpermissive] AppSystem::AppSystem(向量 &appSystemVector){ ^~~~~~~~~ 在 AppSystem.cpp:8:0 包含的文件中: AppSystem.h:21:32: 注意: 'std::vector& AppSystem::ApplicationVector' 应该被初始化 向量 &ApplicationVector;

有什么建议吗?

【问题讨论】:

  • 为什么ApplicationVector 是参考?
  • vector&lt;Application &amp;&gt; &amp;ApplicationVector; -- 为什么都是这些引用?为什么不是简单的vector&lt;Application&gt; ApplicationVector;
  • 1) 创建引用向量是不合法的。 2) 必须初始化引用成员变量。你只是稍后分配它。查找构造函数成员初始化列表,可能还有std::reference_wrapper
  • 当我做矢量 appVector(appTable);我正在尝试初始化它
  • @Konstantinos 就像丹尼尔所说,你不能创建引用向量。但是你可以创建一个reference_wrappers 的向量。但是你为什么要使用引用呢?

标签: c++ object vector constructor


【解决方案1】:
AppSystem::AppSystem(vector<Application &> &appSystemVector){
  ApplicationVector = appSystemVector;
} 

这不是您可以初始化作为引用的字段的方式。语法是:

AppSystem::AppSystem(vector<Application &> &appSystemVector) : ApplicationVector(appSystemVector) {
} 

这是因为当进入 ctor 的块时,字段已经构建,因此 ApplicationVector 未初始化,这是不可能的。您正在尝试分配一个与 initializing 不同的引用。给出的语法让您可以指定字段的初始化方式。

还要注意:

  • 引用向量是不可能的(出于完全相同的原因,只能初始化引用)
  • 使用变量的标准大小写:applicationVector 而不是ApplicationVector,字段的首字母通常小写。这是约定俗成的问题。

【讨论】:

    【解决方案2】:

    这是不可能的:

    int y = 0;
    int& x;
    x = y;
    

    必须初始化引用才能引用某些内容。同样在课堂上这是不可能的:

    struct broken {
        int& x;
        broken(int& a) {
            x = a;
        }
    }
    

    在执行构造函数的主体之前初始化成员。一般来说,您应该更喜欢在构造函数成员初始化列表中进行初始化,而不是在构造函数中赋值:

    struct fixed {
        int& x;
        fixed(int& a) : x(a) {}
    };
    

    但是,vector&lt;Application &amp;&gt; 也是不可能的。您不能拥有引用容器。如果您仍然需要它,您可以使用std::reference_wrapper。但是,我怀疑您实际上并不需要任何参考资料:

    class AppSystem{
    private:
        vector<Application> ApplicationVector;
    public:
        AppSystem(const vector<Application> inp) : ApplicationVector(inp) {}
    };
    

    【讨论】:

      猜你喜欢
      • 2012-12-01
      • 1970-01-01
      • 2017-02-21
      • 2018-10-07
      • 2012-05-16
      • 2020-03-04
      • 2013-01-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多