How to declare Class name from another variable

I want to do something like this,

1
2
3
4
5
6
void create_database(string A,string filename)
{
Database A;
.
.
.


Any idea how can i do something like this?
Something in your code defines the Database object. Put it above this function.
Defination of Class Database is right above this function.
i am getting this error.

1
2
3
In file included from main.cpp:6:0:
database.cpp: In function ‘void create_database(std::string, std::string)’:
database.cpp:63:10: error: declaration of ‘Database A’ shadows a parameter
This means that there already is an object named A (i.e. the input string object, which is named A). Rename the Database object to something not already used.
Last edited on
But thats what i want. I want to take name of database as input, pass it as parameter in function create_database. And then declare a database name that variable!

I successfully managed to use filename as variable like below, but can't do the same for database name.

1
2
3
4
5
6
7
8

void create_database(string A,string filename)
{

Database A;
ifstream input;
input.open(filename.c_str());
Last edited on
This cannot be done and makes no sense at all, and since the names of objects don't exist in the compiled code, is completely pointless anyway.

If you have a string called A and a database called A, how is the compiler supposed to know which one you mean when you try to use object A?

I successfully managed to use filename as variable like below

No, you did not. There is only one variable named filename. You're trying to make two variables named A.
Last edited on
Ok, Thanks for all your help.

Means i must have to define another class which contain class database as its class variable.
Or you could just give the Database variable a different name. Why not "B"? Once the code is compiled, the names cease to exist anyway.
Look at how you used filename

If you were to have written your ifstream input like this instead, you would have the same problem with filename as you are seeing with A

1
2
3
4
5
void create_database(string A,string filename)
{

Database A;
ifstream filename;

See how filename now "shadows" just like A, this is what Moschops is saying. You named the ifstream "input", as Moschops has stated why not give it a different name, heck even databaseA would work if you "need" the A.
Last edited on
ok, i understand now, thank you.
Topic archived. No new replies allowed.