Это не совсем ясно, что вы пытаетесь сделать здесь, за исключением, возможно обучение основной C++ строит. Вот вам какой-то код.
#include <iostream>
#include <string>
#include <map>
using namespace std;
class TEST
{
public:
//Constructor - sets member string to input
TEST(string input) : _name(input)
{
}
//Destructor called when object goes out of scope
~TEST()
{
cout << "Hi from destructor" << endl;
}
//Variable stored by the class
string _name;
};
int main()
{
string inputString;
cout << "Give a object name";
cin >> inputString;
// Give name to your class instance through the constructor
TEST foo(inputString);
// Store a copy of the object "foo" in a map that can be referenced by name
map< string, TEST > userNamedObjects;
userNamedObjects.insert({ inputString, foo });
// Access the object's data based on user input name
cout << "From map: " << userNamedObjects.at(inputString)._name << endl;
// Sanity check
cout << foo._name << endl;
// Or set it directly
foo._name = "Patrick Swayze";
cout << foo._name << endl;
// The stored object doesn't change, because it's a copy
cout << "From map: " << userNamedObjects.at(inputString)._name << endl;
return 0;
}
Сообщения об ошибках существуют по причине. – LogicStuff
дайте мне причину и решение этой проблемы –
@AnkitAbhinav Вы не можете использовать символ переменной 'name' дважды в той же области. –