|
EDITED
1. I thought 'foreach" was a C++ "feature" - in QT doc they call it macro "Q_FOREACH" -
not sure which way is up...
2. The syntax
foreach(QString str, StringArray[MAX_ARRAY])
is the one which "assigns" INDIVIDUAL char to str
I need the entire string of specific array , not the char
Irregardless if it is a macro or not
what is correct syntax to get the entire string USING "foreach" ?
From the description - it is an iterator and the object of iteration is vaguely described -
sort of "automatic". .
If I iterate QString and expect QString -
one usage gives me full string - from start to last new line
and another gives me INDIVIDUAL characters- one by one.
What am I doing different ?
modified 30-Aug-22 1:48am.
|
|
|
|
|
Did you try to compare your these two "usages" to see the difference between them?
|
|
|
|
|
|
Quote: 1. I thought 'foreach" was a C++ "feature" - in QT doc they call it macro "Q_FOREACH" -
not sure which way is up... Then you were wrong. C++ has NO foreach construct, however, it provides the range-based for , see Range-based for loop (since C++11) - cppreference.com[^].
Quote: 2. The syntax
foreach(QString str, StringArray[MAX_ARRAY]) I cannot find the StringArray class in QT documentation. Do you meant QStringList Class | Qt Core 6.3.2[^]?
On the other hand, if you use a (C -like) array of QString s, then you could use the C++ range-based for .
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
Thanks, after few RTFM I finally came to realize it is QT macro. If I knew that such I should have study the macro and realize I was expecting wrong result. After all this I think it has , the QT macro, its place , but it essentially is a replacement for standard C "for loop ".
Lesson learn.
|
|
|
|
|
You are welcome.
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
It looks I was totally of.
foreach(CommandString,BT_Setup[CommandIndex] )
works as expected since the BT_Setup is QStringList .
It does what it is designed and with QString pulls separate characters.
So the moral of the story - convert QString to QSTringList...
|
|
|
|
|
QString offers a standard C++ iterator: QString Class | Qt Core 6.3.2[^]. So (assuming qstr is the reference to your string) you can try something like:
for (QString::iterator it = qstr->begin(); it != qstr->end(); ++it)
{
}
|
|
|
|
|
I am dealing with a school exercise in c++ within an Ubuntu virtual machine. I have a header.h with the class, a header.cpp with the method implementation, a main.cpp and i compile it (main.cpp includes header.cpp which includes header.h) with the following: g++ main.cpp -Wall -o main (from ubuntu terminal). Then I run it and that's ok.
I then tried to import the same exercise in visual studio 2019 creating at first an empty c++ project. After I tried with a CMake project. In both cases compiler/linker return error LNK2005 relevant to all the method implementation saying that they are already defined in main.cpp.obj.
I would attach the complete project in both versions (Ubuntu side and vstudio19 side) but I do not see how. Regards
|
|
|
|
|
Roberto64_Ge wrote: main.cpp includes header.cpp which includes header.h
This is probably the source of your problem. Visual Studio is probably compiling header.cpp and creating a separate object file, then compiles main.cpp, which includes header.cpp, and so creates two copies of the methods defined in header.cpp. In general it is a mistake to #include a cpp file in another cpp file. You main.cpp only needs to include header.h, which should only provide a declaration of your class. You would then compile main.cpp and header.cpp separately.
You can do this on the ubuntu command line using
g++ -Wall -Wextra main.cpp header.cpp -o main or alternatively
g++ -Wall -Wextra -c main.cpp
g++ -Wall -Wextra -c header.cpp
g++ main.o header.o -o main In the above example, the -c option to g++ tells the compiler to only produce an intermediate object file (e.g. a .o file), and not try to produce an executable. The third line creates the final executable, and includes all the system libraries and startup code needed for the executable.
Keep Calm and Carry On
|
|
|
|
|
Thank you, your answer is clear. Now the point is that I am not used at all in c++ compiling/linking and I am not used to VS configuration of compiler and linker, so I ask if you could help. Correct me if I am wrong : (we are within VS2019)
1) a create a new cc++ emty project
2) Add main.cpp , header.cpp and header.h to the project.
3) Include header.h in main.cpp
Now from the VS2019 environment, how do I compile separately main.cpp and header.cpp? I guess I have to compile header.cpp before. Isn't it? But how do I do that?
And then, how do I compile main.cpp and how the VS2019 knows, while compiling main.cpp, where to get the method implementation i.e. the header.obj?
|
|
|
|
|
If you've created a VS C++ project, it knows how to deal with files that you add to the project. All you should need to do for a simple project is hit Build, and the IDE will take care of the rest.
Keep Calm and Carry On
|
|
|
|
|
Ok I tried and I did it. I right clicked on header.cpp and it gave the option to compile. This created the header.obj. Then I right clicked on the main.cpp and again I had the option to compile it and this generated the exe. Before doing all this I included in the main.cpp only header.h,
Now it works. Thanks for the clear explanation
|
|
|
|
|
|
|
Roberto64_Ge wrote: main.cpp includes header.cpp which includes header.h This means that all the definitions in header.cpp will be duplicated in the object files. Do not include .cpp files in others, only include the actual headers, i.e. in this case main.cpp should only include header.h.
|
|
|
|
|
|
I there a way to track down the linker errors further than what`s being offered in the "Build output" window?
|
|
|
|
|
Not really. You can increase number of linker messages by changing the "Show Progress" option. The "Display all messages" is really verbose.
Otherwise, sometimes they can be tricky to trace. Just yesterday I had one where an include directive was used after a
namespace blah_blah { line. Linker was looking for a function definition inside the namespace when it was actually outside.
Mircea
|
|
|
|
|
If you actually provide some details then maybe we can help.
|
|
|
|
|
I`m not sure what details you are expecting. All I have in the build output window is a one line message:
1>Tutorial07.obj : error LNK2001: unresolved external symbol "void __cdecl CheckCollisions(class std::vector<class gameelement="" *,class="" std::allocator<class="" *=""> > *,int,class contim *,int)"
I know an error of this type means that some parameters don`t match in the function definition, declaration and the place where the function is being used. I did a search for the function name within the code file, everything seems to be fine in the function header (return type, function name and parameters) everywhere. I need more details to know what exactly doesn`t match.
void CheckCollisions(vector<gameelement * > * GAllElements, int UnitCount, contim* OldNewTable,int frame);
never mind I found the error, it was a parameter mismatch, you have to be extra careful with those.
modified 27-Aug-22 15:51pm.
|
|
|
|
|
I have a json file genrated in python from another system that needs to be read/updated in another MFC application. I can successfully read the existing data from the json file using nlohmann json add-in. For example, the file consists of two data fields with one field having any array of two subfields as below
{
"city_data":[
{
"t":"m",
"l":[12.0,10.3,0.0,1.0]
},
{
"t":"l",
"l":[10.1,20.37,0.0,1.0]
},
{
"t":"l",
"l":[47.82,4.63,0.0,1.0]
},
{
"t":"m",
"l":[67.66,43.33,0.0,1.0]
}
],
"map_data":"JZDKZTCaTyWQymUwmk8lkMplMJpPJZDKZTCaTyWQymUwmk/8/+n8AVAZ1WCxk8rYAAAAASUVORK5CYII="
}
The "map_data" is basically base64 encoded png image.
I would like to add more entries into "city_data" field as {"t":"x","l":[0.0,0.0,0.0,0.0]} while the "map_data" stays the same. Then I can write the json object into a new file with updates.
To read the json file,
std::ifstream f(file);
json data = json::parse(f);
To retreive "city_data" and then its type and location
json& cityInfo = data["city_data"];
for (unsigned int i = 0; i < cityInfo.size(); i++)
{
json& cityType = cityInfo[i];
std::string ctyTyp = cityType["t"];
json& cityLoc = cityType["l"];
std::float_t x = cityLoc[0];
std::float_t y = cityLoc[1];
}
ofstream outfile;
outfile.open(m_strFolderPath + m_strFileName + ".png", ofstream::binary);
std::string mapFrame = data["map_data"];
string temp = base64_decode(mapFrame );
outfile.write(temp.c_str(), temp.size());
outfile.close();
f.close();
Any suggestions on how to insert new city_data fields into the nlohmann JSON object? or a way to create a new json file like in above format?
thanks
PKNT
modified 26-Aug-22 14:17pm.
|
|
|
|
|
I haven't used nlohmann json in a while (because I've made my own - see mlib/json.h at master · neacsum/mlib · GitHub[^]
However I think this should work:
cityInfo[i]["newval"] = to_string (i + 1);
--EDIT--
I read your message again and realized you want to extend the cityInfo array. In this case you would simply write something like:
cityInfo[4] = json::parse(R"({"t":"x", "l" : [0.0, 0.0, 0.0, 0.0] })");
Mircea
modified 26-Aug-22 21:09pm.
|
|
|
|
|
Hi
I have some question about the class. I would like to use it on My CRichEditctrl where the source code is for my debugger.
There are CWnd members in a number of the methods in the class would that in my case be the CrichEditCtrl in Addition the HiTest api which has a CPoint member is that point relative to (in my case) the CRichEditCtrl
Thanks
|
|
|
|
|
Umm, could you formulate your question(s) a bit more clear?
|
|
|
|