在 c++ 的构造函数中分配对象向量时出错

Error while assigning a vector of object in constructor in c++

本文关键字:对象 向量 出错 分配 c++ 构造函数      更新时间:2023-10-16

我有这个代码:

#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(vector and appSystemVector({  ^~~~~~~~~ 在 AppSystem 包含的文件中.cpp:8:0: AppSystem.h:21:32:注意:"std::vector&AppSystem::ApplicationVector"应该初始化  矢量和应用矢量;

有什么建议吗?

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

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

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

这是因为当进入 ctor 的块时,字段已经构造好了,因此ApplicationVector未初始化,这是不可能的。您正在尝试分配一个与启动它不同的引用。给定的语法允许您指定如何初始化字段。

另请注意:

引用
  • 向量是不可能的(出于完全相同的原因,引用只能初始化(
  • 使用变量的标准大小写:applicationVectorApplicationVector,字段的第一个字母通常是小写的。这是惯例问题。

这是不可能的:

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<Application &>也是不可能的。不能有引用的容器。如果您仍然需要它,您可以使用std::reference_wrapper.但是,我怀疑您实际上并不需要任何参考资料:

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