Create/Initialize new object using variables - c++

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?

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.

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.

Related

C++ Unable to print the entire list in a that has already been created and read from a text file

Hello I have been having trouble with my program I've created a list and I have the following program
Its goal is to Read the 5 variables inside the text file 5 variables in 5 different inputs
so I manage to correctly show the list inside the while loop since every time a line is read it's printed untill .eof(End Of File)
My goal is that I am trying to print the list OUTSIDE of the while Loop that has already read and printed those 5 variables 10x the problem is that it repeats the last entered 5 Variables in the list
I and repeats that 10x (size of the list)
I've also tried something like this inside the for loop which I found in here:
int ID=it->ID;
string NAME=it->NAME;
int SEMESTER=it->SEMESTER;
string DIRECTION=it->DIRECTION;
double GRADE=it->GRADE;
As if those are nodes but I am a starter with nodes as well as lists and it seems to have failed
When I look at the code you've provided, I see that in your for loop the a_student is used, which is not updated by/with the iterator. Basically you're not looping over the created student list.
Maybe try using std::for_each (cppreference) it will save you a lot of time:)
Godbolt example: https://godbolt.org/z/fQUhcr
The issue on the left column of code is that you are not de-referencing the iterator. You never set the a_students variable inside the loop, so why do you expect it to change on each iteration?
Write something like:
a_students = *it;
inside the loop. However there is a simpler way. Instead of manually handling the iterators, use:
for (auto a_students: s_stl_list)
{
// do something with each "a_students" which will be AUTOmatically the right type
}

c++ copy a value to one string and it populated another

I have a function in which I populate some string variables. problem is I set 6 vars with source and destination path,names and extensions, I set a further two variables and one of the file varaibles changes as well.
lAppendStr.assign(ID_MONGODB_APPEND); // "smallfiles=true\0";
lSearchStr.assign(ID_MONGODB_APPEND); // "smallfiles=true\0";
lSrcPath.assign(ID_MONGOODB_PATH); // "/etc/\0" ;
lSrcName.assign(ID_MONGOODB_NAME); // "mongodb\0";
lSrcExt.assign(ID_MONGOODB_EXT); // ".conf\0";
lDestPath.assign(ID_MONGOODB_PATH); // "/etc/\0";
lDestName.assign(ID_MONGOODB_PATH); // "mongodb\0";
lDestExt.assign(ID_DEFAULT); // "def";
when .def is assigned, lAppendStr is also populated with .def
All vars are strings and initialised with "" the ID definitions are terminated with \0 but only as a clutch at straws operation.
I have tried using the string values instead of the ID, and moving the list around changes the assignment results but the variables are still getting cross contaminated.
This is obviously part of larger code, but I guess in my mind is a memory boundary problem. I also posted an issue regarding a call to construct a class, which feels like a it could be in the same vein.
This is LINUX os developed on a MacBookair, in an eclipse oxygen dev editor environment.
Any guidance would be greatly appreciated.

Getting `illegal pointer` error when using AddFriend from ROOT(cern)

Aim;
I wish to compare the contents of two ROOT TTree objects that have identical structure (but not identical contents obviously). The best way to do this seems to be using AddFriend.
Problem;
I am getting this error message;
Error: illegal pointer to class object t3 0x0 1681 makeFriends.C:6:
*** Interpreter error recovered ***
What I've tried so far;
After running the example at the bottom of this page successfully I decided to reduce it down to just the reading and friend adding section, as I had already created tree3.root and tree3f.root in the first run. So I had a file called tree3.C containing;
// Function to read the two files and add the friend
void tree3r() {
TFile *f = new TFile("tree3.root");
TTree *t3 = (TTree*)f->Get("t3");
// Add the second tree to the first tree as a friend
t3->AddFriend("t3f","tree3f.root");
// Draw pz which is in the first tree and use pt
// in the condition. pt is in the friend tree.
//t3->Draw("pz","pt>5");
}
This worked as expected, when loaded (root[] .L tree3.C) and run (root[] tree3r()) from the root prompt.
So I put a copy in a folder containing both of my root files, plainMaskOutput.root and DNMaskOutput.root, and changed strings in the copy to match the names of my files. So I have;
// Function to read the two files and add the friend
void tree3r() {
TFile *f = new TFile("plainMaskOutput.root");
TTree *t3 = (TTree*)f->Get("t3");
// Add the second tree to the first tree as a friend
t3->AddFriend("t3f","DNMaskOutput.root");
// Draw pz which is in the first tree and use pt
// in the condition. pt is in the friend tree.
//t3->Draw("pz","pt>5");
}
Which gives the error above. I dont understand why these things are behaving diffrently? Why can't they just be friends?
It turned out that TFiles method Get might return null, indicating the failure. You were not taking that into account. Why does it return null in your case?
According to the link I provided in the comments (https://root.cern.ch/phpBB3/viewtopic.php?t=12407) it is because your file doesn't contain a tree having a name you provided.
It will be better to add an explicit check of the returned value from Get. if the file gets changed later, your program will start to crash again.
The problem is that plainMaskOutput.root is a file name and the string inside the Get() parenthesis is the tree name. The file called plainMaskOutput.root did not contain a tree by the name t3 it contained one by the name HitsTree. So the line should have been;
TTree *foo = (TTree*)f->Get("HitsTree");
Similarly, the add friend command needed to have the name of the tree stored in DNMaskOutput.root, but as they have the same name it should be aliased;
foo->AddFriend("DNHitsTree = HitsTree","DNMaskOutput.root");
This is just the problem I was having this time, it may not always be the problem associated with this error. I am not knowledgeable enough in this area to say what other problems are possible.

python win32com shell.SHFileOperation - any way to get the files that were actually deleted?

In the code I maintain I run across:
from win32com.shell import shell, shellcon
# ...
result,nAborted,mapping = shell.SHFileOperation(
(parent,operation,source,target,flags,None,None))
In Python27\Lib\site-packages\win32comext\shell\ (note win32comext) I just have a shell.pyd binary.
What is the return value of shell.SHFileOperation for a deletion (operation=FO_DELETE in the call above) ? Where is the code for the shell.pyd ?
Can I get the list of files actually deleted from this return value or do I have to manually check afterwards ?
EDIT: accepted answer answers Q1 - having a look at the source of pywin32-219\com\win32comext\shell\src\shell.cpp I see that static PyObject *PySHFileOperation() delegates to SHFileOperation which does not seem to return any info on which files failed to be deleted - so I guess answer to Q2 is "no".
ActiveState Python help contains SHFileOperation description:
shell.SHFileOperation
int, int = SHFileOperation(operation)
Copies, moves, renames, or deletes a file system object.
Parameters
operation : SHFILEOPSTRUCT
Defines the operation to perform.
Return Value
The result is a tuple containing int result of the
function itself, and the result of the fAnyOperationsAborted member
after the operation. If Flags contains FOF_WANTMAPPINGHANDLE, returned
tuple will have a 3rd member containing a sequence of 2-tuples with
the old and new file names of renamed files. This will only have any
content if FOF_RENAMEONCOLLISION was specified, and some filename
conflicts actually occurred.
Source code can be downloaded here: http://sourceforge.net/projects/pywin32/files/pywin32/Build%20219/ (pywin32-219.zip)
Just unpack and go to .\pywin32-219\com\win32comext\shell\src\

Boost Shared Memory Map reattach

My Issue is the following:
Why can my program not re-attach to the shared memory map?
I do the following in my program(it might be easier to use the example from the boost page for you while this is only a small fragment from my program):
First time, initialize it:
m_sharedMemory = new managed_shared_memory(create_only, segmentName.c_str() , 1000000);
m_hashMap = m_sharedMemory->construct<MyHashMap>(segmentName.c_str())( 3, boost::hash<std::string>(), std::equal_to<std::string>() , m_sharedMemory->get_allocator<ValueType>());
Second time "Re-Attach"
m_sharedMemory = new managed_shared_memory(open_only, segmentName.c_str());
m_hashMap = m_sharedMemory->find<MyHashMap>(segmentName.c_str()).first;
My Issue here is ,if 2 items get inserted the .second call on the returned object from find will show "1" which is actually wrong, its supposed to show 2, after this if my program tries to find anything in the stored map the program crashes. Has somebody been doing this already.
If i do the same thing in the initial program run it is no problem to lookup values from the hash. This only occurs if the program was initialized and later the program is restarted and does the attach and tries to retrieve values formerly inserted.
Thanks for the help.
Boost Quick Ref Map Example
While i was talking to the "maker" of this library he told me that it will only be possible to use the map within the same process.