c++ - 在 C++ 的构造函数中分配对象向量时出错
问题描述
我有这个代码:
#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;
有什么建议吗?
解决方案
AppSystem::AppSystem(vector<Application &> &appSystemVector){
ApplicationVector = appSystemVector;
}
这不是您可以初始化作为引用的字段的方式。语法是:
AppSystem::AppSystem(vector<Application &> &appSystemVector) : ApplicationVector(appSystemVector) {
}
这是因为在进入 ctor 的块时,字段已经构建,因此ApplicationVector
未初始化,这是不可能的。您正在尝试分配一个与初始化它不同的引用。给出的语法允许您指定字段的初始化方式。
另请注意:
- 引用向量是不可能的(出于完全相同的原因,只能初始化引用)
- 使用变量的标准大小写:
applicationVector
不是ApplicationVector
,字段的第一个字母通常是小写。这是约定俗成的问题。
推荐阅读
- typescript - 如何从打字稿中的对象键创建对象类型?
- react-native - React Native - 更新由管理的视图的属性“nativeBackgroundAndroid”时出错:RCTView
- java - 在 Java 中使用 Matlab 结构数据类型
- javascript - 反应本机应用程序关闭/终止时的警报提示
- python - 如何在python(tkinter)中计算条目中的输入
- ckeditor - ckeditor - 使用装饰器将标题标签添加到链接
- c++ - 对自定义结构数组进行排序时编译错误
- elasticsearch - 如何在 Kubernetes 部署文件中覆盖 pods 容器中的文件?
- asp.net - 更新“酒店”对象的 Put 方法以 System.Text.Json.JsonException 结尾
- perl - 如何包装需求和使用?