Page 1 of 1
If you have time, help with code ?
Posted: Mon Oct 04, 2010 8:39 am
by PaperDuckyFTW
Heeya, I guess out of fun primarily, I'm working on a Level class to load and draw the levels of a game.
I've hit a problem with the code and just thought someone here will definately know the answer so here's the problematic pile of ass:
Code: Select all
Level::Level(int htn, int vtn, const string &filename){
MAPX = htn;
MAPY = vtn;
fname = filename.c_str();
}
htn means horizontal tile number, and vtn means verticle tile number. fname is a const string& much like the function variable.
THe errors it gives me when I compile are these:
error C2758: 'Level::fname' : must be initialized in constructor base/member initializer list
no operator found which takes a left-hand operand of type 'const std::string'
I've never initialised a const string to another const string so im not sure whats up, nor am i not suprised somethings wrong. Like the title says, if you have the time, i'd be greatful. Other then that thankyou very much and take care yaoo.
Btw, if you're from Australia (like me :D ) Pure Pwnage is now on TV!! Ive seen every episode but now I dont have to rely on my crappy internet which has past its dl peak to watch them online. Who else has heard of PP?
Re: If you have time, help with code ?
Posted: Mon Oct 04, 2010 9:41 am
by dandymcgee
PaperDuckyFTW wrote:Heeya, I guess out of fun primarily, I'm working on a Level class to load and draw the levels of a game.
I've hit a problem with the code and just thought someone here will definately know the answer so here's the problematic pile of ass:
Code: Select all
Level::Level(int htn, int vtn, const string &filename){
MAPX = htn;
MAPY = vtn;
fname = filename.c_str();
}
htn means horizontal tile number, and vtn means verticle tile number. fname is a const string& much like the function variable.
THe errors it gives me when I compile are these:
error C2758: 'Level::fname' : must be initialized in constructor base/member initializer list
no operator found which takes a left-hand operand of type 'const std::string'
I've never initialised a const string to another const string so im not sure whats up, nor am i not suprised somethings wrong. Like the title says, if you have the time, i'd be greatful. Other then that thankyou very much and take care yaoo.
Btw, if you're from Australia (like me :D ) Pure Pwnage is now on TV!! Ive seen every episode but now I dont have to rely on my crappy internet which has past its dl peak to watch them online. Who else has heard of PP?
It has to either be initialized in the constructor's initialization list, or non-const. Either should work just fine for a filename.
Code: Select all
class Level
{
private:
const std::string fname; //remember consts are required to be initialized right at the time of declaration
public:
Level(int htn, int vtn, const string &filename) : fname(filename), MAPX(htn), MAPY(vtn) {}
}
Re: If you have time, help with code ?
Posted: Mon Oct 04, 2010 10:17 am
by PaperDuckyFTW
Thankyou very much for your help, as expected it worked :D
This is more proof I gotsta go back to learning the more advanced c++ that i skipped out on. However, now MSVC++ is spewing out a ton of errors saying things like:
"unions SDL_Event event already defined in main.obj; second definition ignored
**Edit: to be precise: error LNK2019, LNK1120 nad LNK4006 (if you've heard of the error)
And for all globals such as screen dimensions and surfaces. Has anyone come accross this befre? I solved it once so I think it would have been best to add the site to my favorites... ">.>
Re: If you have time, help with code ?
Posted: Mon Oct 04, 2010 11:16 am
by kamokow
PaperDuckyFTW wrote:Thankyou very much for your help, as expected it worked :D
This is more proof I gotsta go back to learning the more advanced c++ that i skipped out on. However, now MSVC++ is spewing out a ton of errors saying things like:
"unions SDL_Event event already defined in main.obj; second definition ignored
**Edit: to be precise: error LNK2019, LNK1120 nad LNK4006 (if you've heard of the error)
And for all globals such as screen dimensions and surfaces. Has anyone come accross this befre? I solved it once so I think it would have been best to add the site to my favorites... ">.>
You're defining your event structure twice, probably because you included its declaration improperly. If your event structure is declared in a header you should probably do something like this:
events.hpp:
Code: Select all
#ifndef __EVENTS_HPP
#define __EVENTS_HPP
#include "SDL/SDL.h"
extern SDL_Event event;
#endif __EVENTS_HPP
events.cpp:
Code: Select all
#include "SDL/SDL.h"
SDL_Event event;
And of course the same ideas apply to your global variables like screen attributes and stuff.
Re: If you have time, help with code ?
Posted: Mon Oct 04, 2010 7:41 pm
by PaperDuckyFTW
Thanks for your reply, but it didnt work
I made all the bothersome variables into extern variables, then made a .cpp file where I declared them as usual however it still gives me the same errors. Just from, curiosity, why does MSVC++ make a .obj files anyway? I've looked through them and it looks as if they just make a copy of what is in inside the .dll
Re: If you have time, help with code ?
Posted: Mon Oct 04, 2010 8:26 pm
by Ginto8
PaperDuckyFTW wrote:Thanks for your reply, but it didnt work
I made all the bothersome variables into extern variables, then made a .cpp file where I declared them as usual however it still gives me the same errors. Just from, curiosity, why does MSVC++ make a .obj files anyway? I've looked through them and it looks as if they just make a copy of what is in inside the .dll
well, to understand that you have to understand how compilation works. It goes something like this:
raw code -> [compilation] -> object files -> [linking] -> executable
a .obj file is an object file, and it's basically the program itself, except it doesn't contain everything. That data can be gotten from other object files, or outside libraries during the linking stage. Once that's done, it's all brought together into one executable file
Re: If you have time, help with code ?
Posted: Mon Oct 04, 2010 8:29 pm
by kamokow
PaperDuckyFTW wrote:Thanks for your reply, but it didnt work
I made all the bothersome variables into extern variables, then made a .cpp file where I declared them as usual however it still gives me the same errors.
Are you including a .cpp file somewhere? As a general rule you dont do that anyway, but if you are it could also cause multiple declaration errors.
Just from, curiosity, why does MSVC++ make a .obj files anyway? I've looked through them and it looks as if they just make a copy of what is in inside the .dll
Its not just MSVC++, most compilers produce object code. If you are really interested in what they are used for and what not, I'm sure you could find quite a bit with a simple google search.
Re: If you have time, help with code ?
Posted: Mon Oct 04, 2010 9:50 pm
by PaperDuckyFTW
I know about the .cpp include rule and so all I've done is included the .h files
Ive played around with it, and decided to just put everything in a .h file instead of having a .cpp file for each header. Now its giving me:
error LNK2019: unresolved external symbol _SDL_main referenced in function _main
Level Class.exe : fatal error LNK1120: 1 unresolved externals
Its time for a childish FML rage on facebook
Re: If you have time, help with code ?
Posted: Tue Oct 05, 2010 11:38 am
by Milch
Try linking SDL_main ( or a lib named similar - don't have SDL on my pc )
This kind of error happens if you ( or a lib you link ) uses a function that is not found.
Re: If you have time, help with code ?
Posted: Tue Oct 05, 2010 1:08 pm
by PaperDuckyFTW
/facepalm, /chicken, /imadumbass
In the main funtion, when you initialse/declare it, you're meant to have (int argc, char* args[] )
However I forgot to have that at all, because for the past month i havnt been using SDL so havn;t needed nything other then main()
Thanks everyone for your help. Meanwhile ima repeat the (int argc, char* args [] ) until the pen im writing in runs out of ink
Re: If you have time, help with code ?
Posted: Fri Oct 08, 2010 12:25 am
by short
PaperDuckyFTW wrote:/facepalm, /chicken, /imadumbass
In the main funtion, when you initialse/declare it, you're meant to have (int argc, char* args[] )
However I forgot to have that at all, because for the past month i havnt been using SDL so havn;t needed nything other then main()
Thanks everyone for your help. Meanwhile ima repeat the (int argc, char* args [] ) until the pen im writing in runs out of ink
Haha don't waste your time just memorizing them, if you understand what they mean then you can just remember that your program accepts parameters and it needs the number of parameters (argc) and the arguments themselves (char* args[]). If you remember WHY there has to be parameters in main you can easily recall them, at least that is how my brain works. Memorizing without understanding is a waste of time imo.
Re: If you have time, help with code ?
Posted: Fri Oct 08, 2010 4:49 am
by kamokow
short wrote:Haha don't waste your time just memorizing them, if you understand what they mean then you can just remember that your program accepts parameters and it needs the number of parameters (argc) and the arguments themselves (char* args[]). If you remember WHY there has to be parameters in main you can easily recall them, at least that is how my brain works. Memorizing without understanding is a waste of time imo.
Definitely this, if you don't understand what you're memorizing then it makes no sense to memorize it. It would be like learning a second language by memorizing a lot of words that you don't know the translation to.
Re: If you have time, help with code ?
Posted: Fri Oct 08, 2010 7:26 am
by PaperDuckyFTW
Both of you are right, and i totally agree with you. After playing with Qt for a bit, i understood why there was parameters in the main function so I have a basic understanding of what it means however im not too fussed as of now learning how to use them. From memory Qt uses it for managing the window or sommit, is taht the same case for SDL? Ill google it now so idk i asked that question...o_O
Re: If you have time, help with code ?
Posted: Fri Oct 08, 2010 2:04 pm
by short
If you have ever run a program using linux/unix, or from any terminal for that matter it looks something like this:
awk -f myFile
the program you are executing is "awk" and the first parameter is -f and the second one is the name of the script that awk opens. This is just an example.
In the main of awk, argc would be equal to two (there are two parameters, -f and myFile) and args would be both strings "-f" and "myFile"
This is how argc and args are used.
note: I've never seen awk.c or awk.cpp so it could be done differently, but this is the general idea.
Re: If you have time, help with code ?
Posted: Sat Oct 09, 2010 12:46 pm
by kamokow
short wrote:If you have ever run a program using linux/unix, or from any terminal for that matter it looks something like this:
awk -f myFile
the program you are executing is "awk" and the first parameter is -f and the second one is the name of the script that awk opens. This is just an example.
In the main of awk, argc would be equal to two (there are two parameters, -f and myFile) and args would be both strings "-f" and "myFile"
This is how argc and args are used.
note: I've never seen awk.c or awk.cpp so it could be done differently, but this is the general idea.
argc would actually be equal to three, the program name is in itself a parameter.
argc-example.cpp:
Code: Select all
#include <cstdlib>
#include <cstdio>
int main(int argc, char* argv[]){
printf("argc: %d\n", argc);
return 0;
}
output:
Code: Select all
[zachary@zachary-fedora]$ ./argc-example two three
argc: 3