If you have time, help with code ?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
PaperDuckyFTW
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 76
Joined: Sat Apr 03, 2010 5:08 am
Programming Language of Choice: C++

If you have time, help with code ?

Post 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?
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: If you have time, help with code ?

Post 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) {}
}
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
PaperDuckyFTW
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 76
Joined: Sat Apr 03, 2010 5:08 am
Programming Language of Choice: C++

Re: If you have time, help with code ?

Post 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... ">.>
User avatar
kamokow
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 68
Joined: Wed Aug 05, 2009 9:36 pm
Programming Language of Choice: C++
Location: Canada

Re: If you have time, help with code ?

Post 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.
Image
PaperDuckyFTW
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 76
Joined: Sat Apr 03, 2010 5:08 am
Programming Language of Choice: C++

Re: If you have time, help with code ?

Post by PaperDuckyFTW »

Thanks for your reply, but it didnt work :evil:
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
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: If you have time, help with code ?

Post by Ginto8 »

PaperDuckyFTW wrote:Thanks for your reply, but it didnt work :evil:
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
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
kamokow
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 68
Joined: Wed Aug 05, 2009 9:36 pm
Programming Language of Choice: C++
Location: Canada

Re: If you have time, help with code ?

Post by kamokow »

PaperDuckyFTW wrote:Thanks for your reply, but it didnt work :evil:
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.
Image
PaperDuckyFTW
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 76
Joined: Sat Apr 03, 2010 5:08 am
Programming Language of Choice: C++

Re: If you have time, help with code ?

Post 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 :roll:
User avatar
Milch
Chaos Rift Junior
Chaos Rift Junior
Posts: 241
Joined: Sat Jul 11, 2009 5:55 am
Programming Language of Choice: C++
Location: Austria, Vienna

Re: If you have time, help with code ?

Post 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.
Follow me on twitter!
PaperDuckyFTW
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 76
Joined: Sat Apr 03, 2010 5:08 am
Programming Language of Choice: C++

Re: If you have time, help with code ?

Post 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
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: If you have time, help with code ?

Post 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.
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
User avatar
kamokow
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 68
Joined: Wed Aug 05, 2009 9:36 pm
Programming Language of Choice: C++
Location: Canada

Re: If you have time, help with code ?

Post 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.
Image
PaperDuckyFTW
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 76
Joined: Sat Apr 03, 2010 5:08 am
Programming Language of Choice: C++

Re: If you have time, help with code ?

Post 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
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: If you have time, help with code ?

Post 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.
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
User avatar
kamokow
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 68
Joined: Wed Aug 05, 2009 9:36 pm
Programming Language of Choice: C++
Location: Canada

Re: If you have time, help with code ?

Post 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
Image
Post Reply