1

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?

1 Answer 1

3

This declaration

DBMS dbms(); 

is a vexing parse. You are not declaring a variable named dbms, but instead you are actually declaring a function named dbms that takes no arguments, and returns a DBMS. Passing this object to a function expecting a DBMS object will not work.

You can fix this with:

DBMS dbms{}; 
Sign up to request clarification or add additional context in comments.

8 Comments

Or just DBMS dbms;
@user253751 Well, yes, in this case, but there are many ways to initialize a variable, and I want to show just one, so I picked uniform initialization.
I see it now, feel lil bit silly of myself. Thank you guys
@HoàngTháiVương No worries, that's why it's vexing :) Consider accepting the answer if it solves your problem.
@Eljay This whole comment section is getting off-topic. Yes, there are many ways to initialize. Is the version in my answer problematic in any way? I don't want to show multiple options without a good reason.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.