0

I am intending to initialize or create a new object using variables. The error says that i am conflicting declaration.

       //instantiate new node object***********************
        string peer = "peer";
        string pNo = convertInt(addPeerNum); //convert peer number to string to concatenate 2 strings
        string NewPeerObject = peer+pNo; << ERROR POINTS TO HERE
        nodes NewPeerObject; << ERROR POINTS TO HERE

Error message:

conflicting declaration 'nodes NewPeerObject' <-- last line of error 'NewPeerObject' has a previous declaration as 'string NewPeerObject' <-- 2nd last line

My main point is to create a new object when I add more peers. If I addpeer 1, it will create a new object 'peer1' If I addpeer 2, it will be 'peer2' etc.

I am reading in the file which contains

 addpeer 1
 addpeer 100
 addpeer 28

In my program, it reads the file and stores the number in a variable called 'addPeerNum' and with me doing this, it actually has a different string content of 'NewPeerObject'.

So in this instance, i am actually trying to create 3 new objects.

Is there any way which i will be able to do it?

1
  • Are you trying to create variables with dynamic names? If so, this is not possible, a variable is given a static name that is only use during compilation. Perhaps what you need is an associative container (a map, for instance) to associate a name with an object? Something like std::map<std::string, Node>? Commented May 14, 2012 at 15:32

2 Answers 2

3

You cannot have two objects with same name like that. It violates the One definition Rule, naturally the compiler complains.
Just please do yourself a favor and change the name of either of them.

Sign up to request clarification or add additional context in comments.

5 Comments

i am intending to create a new object automatically whenever i add a peer. so actually to simply say, 'NewObjectPeer' actually changes everytime when i add a new peer. it is a string and 'addPeerNum' changes everytime.
@Andres With Als on this one. This makes no sense.
@Andres: What your object contains is a different story, the problem is how your objects are named here.You cannot have same names for those objects.
@Als, My variable 'pNo' will be unique and will be always changing. Therefore the final combined string of my variable 'NewPeerObject' is always different in the end
@Andres: Are you trying to create variable name at run-time? You cannot do that.
0

I think that what you are looking for is a kind of dynamically resized array of your objects. You can use std::list to achieve that kind of behavior.

std::list<PeerObject> list;
PeerObject peer(peer_string);
list.insert(peer);

or a std::map if you want to use your peer string as a key

std::map<std::string, PeerObject> peer_map;
PeerObject peer(peer_string);
peer_map.insert(peer_string, peer);
//    .... add more
PeerObject& foundObj = peer_map[a_key];
foundObj.doSomething();

You could also do it with MACROs but only at compile time and you should avoid them if possible.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.