PHP array in C++ ? - Page 2
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 20 of 20

Thread: PHP array in C++ ?

  1. #16
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,012

    Re: Splitting the code to multiple files

    Quote Originally Posted by ArdentAngel View Post
    error C2653: 'aa_physics' : is not a class or namespace name
    error C3861: 'distance': identifier not found
    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.
    Quote Originally Posted by ArdentAngel View Post
    So, do you know any really good C++ programming books?
    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.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  2. #17
    Join Date
    Oct 2012
    Location
    Kromberk, Slovenia
    Posts
    8

    Re: Splitting the code to multiple files

    Quote Originally Posted by D_Drmmr View Post
    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.
    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:air<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.
    Novice to programming in C++.

  3. #18
    Join Date
    Apr 1999
    Posts
    27,424

    Re: Splitting the code to multiple files

    Quote Originally Posted by ArdentAngel View Post
    Could you please assist me? I keep getting errors like:
    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.
    Code:
    #ifndef MYHEADER_H
    #define MYHEADER_H
    //...
    // code
    //..
    #endif
    Open any system header, and you see code like this. I won't go into how this works.

    Here is a simple example:

    Header.h
    Code:
    int x;
    a.cpp
    Code:
    #include "header.h"
    
    void foo();
    int main()
    {
        foo();
    }
    b.cpp
    Code:
    #include "header.h"
    
    void foo()
    {
       x = 10;
    }
    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.

    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
    Last edited by Paul McKenzie; October 14th, 2012 at 03:33 PM.

  4. #19
    Join Date
    Oct 2012
    Location
    Kromberk, Slovenia
    Posts
    8

    Re: Splitting the code to multiple files

    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.
    Novice to programming in C++.

  5. #20
    Join Date
    Oct 2012
    Location
    Kromberk, Slovenia
    Posts
    8

    Re: PHP array in C++ ?

    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
    Code:
    #pragma once
    #include <map>
    #include <string>
    
    struct Coords
    {
    	int x;
    	int y;
    	int z;
    };
    
    extern std::map< std::string, Coords >models;
    globals.cpp
    Code:
    #include "globals.h"
    
    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).

    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 .
    Novice to programming in C++.

Page 2 of 2 FirstFirst 12

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center