0

I have this code:

#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", "[email protected]");
    ApplicationConstructor appConstructor2("3332","Nikos Paulou", "[email protected]");
    ApplicationConstructor appConstructor3("4432","Stavros", "[email protected]");
    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", "[email protected]");
    ApplicationConstructor appConstructor5("1121","Tasos Sotiriou", "[email protected]");
    ApplicationConstructor appConstructor6("4431","Giorgos Papadopoulos", "[email protected]");
    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);
}

And I get this error:

AppSystem.cpp:12:1: error: uninitialized reference member in 'class std::vector&' [-fpermissive]

 AppSystem::AppSystem(vector &appSystemVector){
 ^~~~~~~~~
In file included from AppSystem.cpp:8:0:
AppSystem.h:21:32: note: 'std::vector& AppSystem::ApplicationVector' should be initialized
         vector &ApplicationVector;

Any suggestion?

8
  • 1
    why is ApplicationVector a reference? Commented May 11, 2020 at 18:34
  • 1
    vector<Application &> &ApplicationVector; -- Why are all of these references? Why not a plain vector<Application> ApplicationVector;? Commented May 11, 2020 at 18:34
  • 1
    1) Creating a vector of references is not legal. 2) Reference member variables must be initialized. You just assign it later. Look for constructor member initializer list and possibly std::reference_wrapper. Commented May 11, 2020 at 18:36
  • When I do vector<Application &> appVector(appTable); I am trying to initialize it Commented May 11, 2020 at 18:38
  • @Konstantinos Like Daniel said, you can't create a vector of references. But you can create a vector of reference_wrappers. But why do you want to use references at all? Commented May 11, 2020 at 19:07

2 Answers 2

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

This is not the way you can initialize a field that is a reference. The syntax is:

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

This is because when entering the block of a ctor, fields have been already constructed, thus the ApplicationVector is left uninitialized which is not possible. You are trying to assign a reference which is not the same as initalizing it. The syntax given let you specify how the field is initialized.

Also note that:

  • vector of references is not possible (for the exact same reason, references can only be initialized)
  • use the standard casing of variables : applicationVector not ApplicationVector, first letter is usually lowercase for a field. It's matter of convention.
Sign up to request clarification or add additional context in comments.

Comments

0

This is not possible:

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

References must be initialized to reference something. Also in a class this is not possible:

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

Members are initialized before the body of the constructor is executed. In general you should prefer initialization in the constructors member initializer list over assignment in the constructor:

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

However, also vector<Application &> is not possible. You cannot have containers of references. If you still need it you can use std::reference_wrapper. However, I suspect that you don't really need any references:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.