在 c++ 的构造函数中分配对象向量时出错
Error while assigning a vector of object in constructor in 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(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
未初始化,这是不可能的。您正在尝试分配一个与启动它不同的引用。给定的语法允许您指定如何初始化字段。
另请注意:
引用- 向量是不可能的(出于完全相同的原因,引用只能初始化(
- 使用变量的标准大小写:
applicationVector
不ApplicationVector
,字段的第一个字母通常是小写的。这是惯例问题。
这是不可能的:
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) {}
};
相关文章:
- 为什么我不能将一个对象push_back到属于另一个类的对象向量中?
- 从多个源构造一个对象,包括一个对象向量
- 如何从文件中读取两个字符串和数字数组,并将它们存储在对象向量中
- 对象 C++ 向量的 STL 容器
- 初始化C++中的对象向量
- 从自定义类获取对象向量中的 max 元素
- 如何在 c++ 中打印存档中的对象向量
- 如何将包含另一个对象向量的对象保存到文件中,并使用C++中的二进制文件从文件中读回?
- 尝试重载输出运算符时,我无法遍历对象向量
- 如何在C++中打印对象向量的内容
- 打印出对象向量中的每个元素C++
- 在 c++ 的构造函数中分配对象向量时出错
- 遍历对象向量,并找到与从文本文件中提取的对象匹配的变量
- 对象向量是否在堆或堆栈上分配C++?
- 基于不同字段的对象向量的排序功能
- 有没有一种简单的方法可以在对象向量上调用构造函数?
- 用rapidjson读取子对象向量
- 尝试使用比较运算符对对象向量进行排序
- 如何用对象向量重载 cin(>>)
- 如何将某个元素从shared_ptr擦除到对象向量?