I create an object and try to pass that object through multiple function by reference
#include<iostream> #include <string> #include "DBMS.h" using namespace std; void Home(DBMS &); int main() { DBMS dbms(); Home(dbms); // this is where the error is return 0; } void Home(DBMS &dbms) { string dbName; dbms.addDatabase(dbName); } and this is DBMS.h
#pragma once #include <iostream> #include <string> #include <vector> #include "Database.h" using namespace std; class DBMS { public: DBMS(); void addDatabase(string); Database& getDatabase(string); Database& getDatabaseByIndex(int); int getDatabaseIndex(string); void removeDatabase(string); int size(); ~DBMS(); private: vector <Database> dbList; string error; }; DBMS::DBMS() { } void DBMS::addDatabase(string tbNames) { vector<string> TB_Names = tokenize(tbNames); int size = TB_Names.size(); for (int i = 0; i < size; i++) { if (getDatabaseIndex(TB_Names[i]) == -1) { Database tb(TB_Names[i]); dbList.push_back(tb); } else throw "Already esited Database with given name"; } } Database& DBMS::getDatabase(string tbName) { int i; int size = dbList.size(); for (i = 0; i < size; i++) { if (dbList[i].getName() == tbName) return dbList[i]; } throw invalid_argument("Database not found"); } Database& DBMS::getDatabaseByIndex(int index) { return dbList[index]; } void DBMS::removeDatabase(string TB_Name) { int i; int size = dbList.size(); for (i = 0; i < size; i++) { if (dbList[i].getName() == TB_Name) { dbList.erase(dbList.begin() + i); break; } } if (i == size) throw invalid_argument("Field not found"); } int DBMS::getDatabaseIndex(string TB_Name) { int size = dbList.size(); for (int i = 0; i < size; i++) { if (dbList[i].getName() == TB_Name) return i; } return -1; } int DBMS::size() { return dbList.size(); } DBMS::~DBMS() { } (Database type is just another class i create. Nothing special about it. Don't worry about it (Unless you think I have to)) The error statement is : >a reference of type "DBMS &" (a non-const-qualified) cannot be initialized with an value of "DBMS()" I found a suggestion that i should fix void Home(DBMS &dbms) to void Home(const DBMS &dbms) but if i do that, i can't use addDatabase() function How can i fix this?