Hi, I have a question. How could I create an array that would in PHP look like this:
PHP Code:
$something['argument1']['argument2'] = "some data to store";
Printable View
Hi, I have a question. How could I create an array that would in PHP look like this:
PHP Code:
$something['argument1']['argument2'] = "some data to store";
maybe?Code:std::vector< std::vector< 'type of some data to store' > > something;
See http://cplusplus.com/reference/stl/vector/
But i am only thinking about holding data like coords or something, not some large amounts of sentences. Is there no kind of variable, like char (i tried lots of possibilities with that, but can't really get anywhere ...). What I want to do is a script that calculates distance between 2 points in coordinate system and I want specify which coordinates.
With of course y and z also. ;)PHP Code:
//Models array should only have this kind of values:
$models['object1']['x'] = 7831;
$models['object2']['x'] = 1893;
function distance($objName1, $objName2){
x1 = models[$objName1]['x'];
x2 = models[$objName2]['x'];
//Function that actually does something
}
//And than I simply put in the objects names in the arg field:
distance("object1", "object2");
But I believe you got the point ...
If vectors are the only solution, than I guess I'll go with it ...
Ok so all you need is two objects that can hold the coordinates for a point? If it's just 2 dimensions Windows has POINT. See http://msdn.microsoft.com/en-us/libr...(v=VS.80).aspx
Edit: Ah, but you can easily create your own structure to hold more coordinates.
I think I missed some things here. What you want is probably more like thishttp://cplusplus.com/reference/string/string/Code:
#include <map>
#include <string>
struct Point
{
int x;
int y;
int z;
};
std::map< std::string, Point > models;
int distance( const std::string& objName1, const std::string& objName2 )
{
int x1 = models[objName1].x;
int x2 = models[objName2].x;
//Function that actually does something
return 0;
}
int main()
{
models["object1"].x = 7831;
models["object1"].y = 0;
models["object1"].z = 0;
models["object2"].x = 1893;
models["object2"].y = 0;
models["object2"].z = 0;
int dist = distance( "object1", "object2" );
return 0;
}
http://cplusplus.com/reference/stl/map/
Maybe you also need a if-then-else for
distance["object1","object1"];
as "mapping" fails due to the same key used
To add to SMA's example:
Regards,Code:#include <map>
#include <string>
#include <iostream>
template <typename T>
struct Point
{
T x;
T y;
T z;
Point(T theX = T(), T theY = T(), T theZ = T()) : x(theX), y(theY), z(theZ) {}
};
typedef std::map<std::string, Point<double> > FloatModels;
typedef std::map<std::string, Point<int> > IntModels;
using namespace std;
int main()
{
FloatModels models;
IntModels models2;
models["object1"] = Point<double>(); // defaults to 0.0
models["object2"] = Point<double>(3.0, 2.0, -1.9);
models2["object1"] = Point<int>(); // defaults to 0
models2["object2"] = Point<int>(3, 2, -1);
cout << "Here are the floating point coordinates:\n";
FloatModels::iterator it = models.begin();
while (it != models.end() )
{
cout << it->first << " -> " << "(" << it->second.x << ", " << it->second.y << ", " << it->second.z << ")\n";
++it;
}
cout << "\nHere are the integer point coordinates:\n";
IntModels::iterator it2 = models2.begin();
while (it2 != models2.end() )
{
cout << it2->first << " -> " << "(" << it2->second.x << ", " << it2->second.y << ", " << it2->second.z << ")\n";
++it2;
}
}
Output:
Here are the floating point coordinates:
object1 -> (0, 0, 0)
object2 -> (3, 2, -1.9)
Here are the integer point coordinates:
object1 -> (0, 0, 0)
object2 -> (3, 2, -1)
Paul McKenzie
I assume the underlying datastructure is a hash table. That's available in C++ as an unordered_map. In the newest compilers this will be available in the std namespace (header <unordered_map>), in older compilers it may be available in the std::tr1 namespace.
Thanks, that really helped. Now just to be clear: Why is in every function "return 0" command? (just to learn properly what you guys just pointed me out). I am a novice in C++. I thought it would be ... different ... I was able to get the hang of C# in period of 3 days, so I did not expected C++ to be that much different (specially from reading PHP books, because they always pointed out some other differences ...)
So thanks again to everyone.
It's just because it shall be compilable if somebody copies the code. int distance(...) has to return something but as it is now it is incomplete.
The same goes for int main(). Sometimes you see people use void main and that's not ok according to the standard but accepted by most compilers. I think the reason for it is that in an embedded system there's nobody to return to so void main is quite common. If a watchdog isn't used, returning from main means that the unit has to be power cycled to be restarted.
No, but I did expect to discover by myself how to make the array look the way I asked here at the end. :)
I knew at the very beginning that C++ is advanced language. I never even said that I am a pro at PHP, I am only saying that I did not expect there to be that much of a difference, since with C# I got familiar with (as much as I could for what I had to write in it for a friend who had been learning this in school and none of his classmates knew how to do it. I was his last chance of making it happen (altough I am 3 years younger than he is) ... it wasn't that hard, I admit - I mostly had to get the hang of arrays, which took me 3 hours, since I am a self-learning programmer (if I can call my self that way), so I guess you know how that is ...) quite fast.
But now I see here is a way more than I first expected. :D
Right now for C++ I am using pure google only ... And what I find that way ... :D
So I am open for any book any of you might advise to be a good one.
There is a major difference in C++ and C#, one that may shock you. It is the major difference between C/C++ and most other language that's out there.
In C#, you make a mistake that violates memory in some way, you get a nice error at runtime saying you made a mistake. With C++, you are not guaranteed to get any such error message or warning when you make these mistakes. For example, access an array with an out-of-bounds subscript in C#, then do the same thing in C++. With C++, there is no guarantee that you get an "array subscript error" or similar message at runtime -- the program will run with that bug, and who knows what damage is being done.
The array subscript is just one out of many examples. You have heap management errors, buffer overwrites all caused by a myriad of different reasons, etc.
So you were learning all along "safe" languages (C#, PHP), where you can't make these types of errors without the system telling you that you made these errors. With C++, you're on your own if you make such errors. So you can code all sorts of crap with C++, the code will compile, and may even run, giving you the false sense that everything is OK when it isn't. Run the program on your customer's machine, and everything goes wrong.
To avoid such errors, write code so that it's safe and easily maintainable, etc. requires real experience, not a few days of tinkering around with C++.
Regards,
Paul McKenzie
Thank you all. The code worked perfectly (and I got the hang of it that far). But now I want to split it to multiple header and cpp files. However I keep getting :
error C2653: 'aa_physics' : is not a class or namespace name
error C3861: 'distance': identifier not found
Files are:
main.h
predef.hCode:#include "predef.h"
#include <iostream>
int main()
{
models["object1"].x = 1;
models["object1"].y = 2;
models["object1"].z = 3;
models["object2"].x = 9;
models["object2"].y = 8;
models["object2"].z = 7;
std::cout << aa_physics::distance("object1", "object2");
system("PAUSE");
};
aa_physics.hCode:#include <map>
#include <string>
struct Point
{
int x;
int y;
int z;
};
std::map< std::string, Point > models;
aa_physics.cppCode:#pragma once
#include "main.h"
class aa_physics
{
public:
static int distance(const std::string& objName1 , const std::string& objName2);
};
PS.Code:#include "aa_physics.h"
int aa_physics::distance(const std::string& objName1, const std::string& objName2)
{
int x1 = models[objName1].x;
int x2 = models[objName2].x;
int y1 = models[objName1].y;
int y2 = models[objName1].y;
int z1 = models[objName1].z;
int z2 = models[objName2].z;
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2));
};
So, do you know any really good C++ programming books?
Header files are used for class definitions and function declarations. cpp files are used for (member) function definitions. You include header files in cpp files or in other header files.
So your main.h file should probably be a main.cpp file and you should include "aa_physics.h" in it rather than the other way round.
Since you already have some programming experience, Accelerated C++ by Koenig and Moo will be a good choice. It doesn't cover everything in depth, but it teaches you C++ the way professionals use it from the start.
Could you please assist me? I keep getting errors like:
error LNK2005: "class std::map<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,struct Point,struct std::less<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >,class std::allocator<struct std::pair<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const ,struct Point> > > models" (?models@@3V?$map@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@UPoint@@U?$less@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@V?$allocator@U?$pair@$$CBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@UPoint@@@std@@@2@@std@@A) already defined in aa_physics.obj
error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
fatal error LNK1120: 1 unresolved externals (in builded exe)
PS.
Thanks for book advice. I'll sure check it out.
Those are linker errors, not compiler errors.
Again, the difference between C++ and the languages you used is demonstrated here. In C++, there are two steps in building a program.
1) Compiling -- this is where the syntax is checked, and object files (.obj) files are created for each module that is compiled.
2) Linking -- this is where the linker takes the object files generated and builds the final executable. External functions and variables are resolved here.
The problem is that you created two object modules that declared the same variable. The linker now sees these object modules and the duplicate definitions, thus the error.
The reason for the error is usually that you put a declaration in a header file, and then included this header file in two or more separate, compiled modules. Your header file you posted contains a std::map variable, models. Remove it from the header file and place it in one and only one CPP file.
Which raises another point -- you should not rely on global variables like this. A good C++ program has very little, if any, global variable usage. You now have wandered into the having to know how to use global variables in a multi-module program (the extern keyword), and frankly, usage of extern in a C++ program should be minimized (if ever used).
Your header file also doesn't have #include guards to ensure that two #includes of the header file in one module doesn't cause issues.
Open any system header, and you see code like this. I won't go into how this works.Code:#ifndef MYHEADER_H
#define MYHEADER_H
//...
// code
//..
#endif
Here is a simple example:
Header.h
a.cppCode:int x;
b.cppCode:#include "header.h"
void foo();
int main()
{
foo();
}
Compile a.cpp and b.cpp. You see there are no compiler errors. Now, when it comes to linking, that is when you start to get issues, since the variable "x" is declared twice, once in a.obj, and again in b.obj.Code:#include "header.h"
void foo()
{
x = 10;
}
Again, C++ isn't a 3 or 4 day adventure. It is vastly different than what you're used to, and you need to not use other languages to figure out how to write a C++ program. The "Accelerated C++" book is intended for persons who know how to write programs in other languages only because they are familiar with basic program structure, looping constructs, maybe some procedural code, etc. But by no means does it mean that since you have this experience in other languages, you can "cut corners" and learn C++ in x days. It doesn't work that way.
The final error is that you chose the wrong type of project. You should be selecting "Win32 Console program", and not a Win32 GUI program. The entry point for console programs is main(), and the error you had says "WinMain".
Regards,
Paul McKenzie
Thank you for not giving up on me that easily. I am very grateful. ;)
The thing is, the whole program will be about 2 or more points of which at least one has a fixed position and great attraction. The second point is than going to have a starting speed in some direction, which will keep changing because of the first point (kind of like gravity works). But I am not quite there yet.
Yes, I have created a console project and it have worked until when I cut out the class header and cpp only (and I think I've put the models map there, too). Actually it worked until I decided to make the predef header. That's when I started getting all kinds of errors. I started to figure out where am I linking wrongly (the one I posted here is not the best looking, but not the worst, also).
Normaly I've been using #pragma once, since I heard there is basicaly no difference (except that some compillers won't recognise it ... But I am working with VC2012 RC, so there shouldn't be problems - I'll switch to VC2012 express, once RC won't be available anymore). I've only delited them because I wanted to make sure nothing is included twice (because of those same errors ... my way of solving things ... I guess not the best one ...).
But than again, my only objective was to get hang of arrays in C++ (where what i wanted to do seems possible onky with string & map) in those 3 days. I know I can't learn the while language itself in that short period.
So thanks, I try to figure out what am I doing wrong.
Finally made it. :)
Well, I did started from scratch. First I created an "empty project" (not just ticking the box after you create a "Win32 Project" or "Win32 Console Application"), because sooner or later I will switch from console application to non console one, so I won't had to recreate the whole project (at least I hope so). :)
Than I started splitting it again. This time I had been using "#pragma once", once my project is complete I will change it to #ifndef & #define & #endif. But the problem remained with the includes ... actually mostly because of the global variable that I am planning on using. Maybe this is not the best decision, but since everything will be going around coordinates of a few objects, it is not that bad decision (please tell me if I am wrong). And now I have finally found my problem. It was not ONLY because there was no "extern" parameter to it, but also because I had to call some part of declaring the std::map in the *.cpp file, also (and not only header). Now my global files look like this:
globals.h
globals.cppCode:#pragma once
#include <map>
#include <string>
struct Coords
{
int x;
int y;
int z;
};
extern std::map< std::string, Coords >models;
Than I included this file in my "aa_physics.h" file (used for samenamed class. I also have a "aa_physics.cpp" file) and I have ALSO included it in my "main.cpp" file (I know, include it only once, but I still made that decision. If it is not good decision, please say so. I only thought that might be good, because maybe some other things will still need it, because if I do not include in aa_physics.h I keep getting errors of undefined variable).Code:#include "globals.h"
std::map< std::string, Coords>models;
I hope I've explained everything good enough, but if I am too confusing, just ask for more infos (or the whole code, if you want). Anyway, now I am moving on.
PS.
I am not sure how to change name of a thread, so if anyone think I've better change the thread name, tell me how or to what it should be renamed and I'll point to moderators, if there is no other solution. It is just that this has moved beyond my original intentions and had become so much more. I'd also love to add some more keywords, since I was not allowed to create new ones and adding existing ones seemed kind of confusing (I believe that mostly because it did not worked correctly in IE10 - I am using Win8 Release Preview right now, since I kind of easier multi-task in it). I'd also like to continue asking questions here (about this project) or do you think I'd better create a new one, more depending on the question I'll have to ask on the way?
And of course delete those few lines in case you decide to rename the thread, or whatever, or say it is OK and I'll delete them ;) .