skip to Main Content

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?

2

Answers


  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.
    Login or Signup to reply.
  2. 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) {}
    };
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search