【发布时间】: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<Application &> &ApplicationVector;-- 为什么都是这些引用?为什么不是简单的vector<Application> ApplicationVector;? -
1) 创建引用向量是不合法的。 2) 必须初始化引用成员变量。你只是稍后分配它。查找构造函数成员初始化列表,可能还有
std::reference_wrapper。 -
当我做矢量
appVector(appTable);我正在尝试初始化它 -
@Konstantinos 就像丹尼尔所说,你不能创建引用向量。但是你可以创建一个
reference_wrappers 的向量。但是你为什么要使用引用呢?
标签: c++ object vector constructor