How to set a pointer to an object in set?

Hello all!

I have a std::set<Sense> all_synsets; defined in my main procedure. Afterwards I wanna call a procedure where I can point to an element of the set. How can I do this? This is what I tried but doesn't work...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
std::set<Sense> all_synsets;
 for(sense_iterator=synsets.begin();sense_iterator!=synsets.end();sense_iterator++)
				  {
					
					all_synsets.insert((*sense_iterator));
					
					SetNodeInEdge((*sense_iterator));
					

				  }

...

Sense *Node2;
void SetNodeInEdge(Sense n2)
	{
		&Node2=n2;
	}

How can I make Node2 point to n2??

At the very least line 17 won't work because it points N2 to the address of the parameter of the function which
will vanish when the function returns.
All right I tried *Node2=&n2, but I have difficulties overloading operator = now. I am a C# programmer and I am trying to use C++ and get accustomed to it. Sorry for not being proficient..
JSYK, it is not safe to use pointers to items in STL containers, because the container is free to copy and destroy elements at will. If you must, use a container of pointers to your objects, which you explicitly allocate and free.

std::set <Sense*> all_synsets;

If you do that, you may also find the following link useful:
http://www.wesleysteiner.com/professional/del_fun.html

Good luck!
Thanks! Im not gonna erase or add elements, this is just a way of saving my data. Do you think I should save it in an array and then point to the array elements?
I am using this procedure to initialize two pointers from a class I defined:
1
2
3
4
5
void SetNodesInEdge(Word n1, Sense n2)
	{
		*Node1=&n1;
		*Node2=&n2;
	}


And I have defined opertator = in the Sense class:
1
2
3
4
5
6
7
8
9
 Sense& operator = (const Sense &NewNode)
	{
		this->SetNodeID(NewNode.GetNodeID());
		this->SetNodeLabel(NewNode.GetNodeLabel());
		this->SetExampleUseString(NewNode.GetExampleUseString());
		this->SetNoTagged(NewNode.GetNoTagged());

	  return *this;
	}


But I get the following error:
error C2679: binary '=' : no operator found which takes a right-hand operand of type 'Sense *' (or there is no acceptable conversion)

Why my operator is not recognized?




1
2
3
4
5
Sense n2;
Sense *Node2;
*Node2; //dereferencing, now is a Sense (or derived class) object
&n2; //referencing (memory address) now is a pointer
*Node2=&n2; // Sense = Sense * 
The & is the 'address-of' operator, as in "what is the address of n1?" (Not to be confused with the 'reference-type' type modifier.)

The * is the 'dereference' operator, as in "I wanna mess with the thing addressed by Node1".

Your assignment operator is correct, but you are not asking to use it. Pay attention to the type of thing:

  Node1 is a pointer (to a 'Sense' object) [I think...]
  *Node1 is a 'Sense' object
  n1 is a 'Word' (some kind of integer?)
  &n1 is a pointer (to a 'Word')
  n2 is a 'Sense' object
  &n2 is a pointer (to a 'Sense' object)

Your assignment operator only works with 'Sense' objects. Not pointers or anything else. Hence, you should have:

1
2
3
4
5
void SetNodesInEdge(Word n1, Sense n2)
	{
		*Node1=n1;  // Assuming you have an assignment operator as below:
		*Node2=n2;
	}

The assignment operator you need for Word to Sense:

1
2
3
4
5
Sense& operator = (const Word& word)
	{
		/* Do whatever you need to make a 'Sense' have value from a 'Word'
		return *this;
	} */

Hope this helps.
Topic archived. No new replies allowed.