Re: Splitting the code to multiple files
Quote:
Originally Posted by
ArdentAngel
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
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.
Re: Splitting the code to multiple files
Quote:
Originally Posted by
D_Drmmr
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::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.
Re: Splitting the code to multiple files
Quote:
Originally Posted by
ArdentAngel
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
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
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.