You are not logged in.

#1 2010-05-10 21:09:15

GalacticArachnid
Member
Registered: 2009-01-02
Posts: 155
Website

[Solved] Frustrating C++ error, possibly SDL

Right so trying to compile this little proggy, the main .h file is like this

#ifndef MONOLITH
#define MONOLITH

#include <iostream>
#include <string>
#include <cstdlib>
#include <math.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>

#include "game_objects.h"

[  ...   ]

class monolith
{
    protected:
        SDL_Surface *screen;
    
        int monoInitVideo( Uint32 flags = SDL_DOUBLEBUF | SDL_SWSURFACE, int width = 100, int height = 100 ); 
        SDL_Surface *monoLoadImage( string imageName, int &exitState );

[  ...  ]

I keep getting an eror refering to the monoLoadImage line:

monolith.h:25: error: expected ';' before '(' token

followed by an error about the second line in the main .cpp file (after the include line for that main .h file):

monolith.cpp:3: error: expected unqualified-id before 'using'

referring to this line:

using namespace std;

Any ideas? I truncated code to save space, can paste all of it on request

Last edited by GalacticArachnid (2010-05-13 22:32:28)

Offline

#2 2010-05-10 21:34:09

scio
Member
From: Buffalo, NY
Registered: 2008-08-05
Posts: 366

Re: [Solved] Frustrating C++ error, possibly SDL

Edit 2: I'm dumb, that was the header...

Last edited by scio (2010-05-10 21:53:49)

Offline

#3 2010-05-10 21:39:16

GalacticArachnid
Member
Registered: 2009-01-02
Posts: 155
Website

Re: [Solved] Frustrating C++ error, possibly SDL

Sorry, I don't quite follow you..

the functions shown there are prototypes in the header file (i assumed i can still do this in C++), so it seemed reasonable to be able to assign default values for argument variables in the prototype

oh, also this is my own code. I havent done much C++ before, but i am quite comfortable with C
-- this was ment to be a series of small proggies i was planning to make to get use to C++ before doing some 'real' work. but damn..

Last edited by GalacticArachnid (2010-05-10 21:40:41)

Offline

#4 2010-05-10 21:58:30

scio
Member
From: Buffalo, NY
Registered: 2008-08-05
Posts: 366

Re: [Solved] Frustrating C++ error, possibly SDL

Yes, sorry, I'm a bit brain dead today.  Totally missed that I was looking at a header.

Do you get any warnings about not being able to find headers,

SDL/SDL.h

looks like it should be what you need.
It seems to be ok with

SDL_Surface *screen;

, so if you comment out the monoInitVideo function does it compile?

If possible, toss all the code up on pastie or pastebin.

Offline

#5 2010-05-10 22:04:49

GalacticArachnid
Member
Registered: 2009-01-02
Posts: 155
Website

Re: [Solved] Frustrating C++ error, possibly SDL

Ahh, that's okay xD

Now, this is where it get really interesting (well, much like hitting your head against a wall is 'interesting').

Using

#include <SDL.h>

actually worked just now (previously i kept getting an error about it).. well, nvm, so that works.

Okay, still flags up monoLoadImage

So, I commented it out, and now it tells me:

monolith.cpp:103: error: 'monoCheckBoundingBox' was not declared in this scope

that function is in the header:

    private:
        int monoInitVideo( Uint32 flags = SDL_DOUBLEBUF | SDL_SWSURFACE, int width = 100, int height = 100 ); 
        //SDL_Surface *monoLoadImage( string imageName, int &exitState );
        void monoPrepObject( image_coord_t object );
        void monoDrawImage( SDL_Surface *image, image_coord_t surface_param, image_coord_t dest_param );
        
        int monoCheckBoundingBox( image_coord_t obj1, image_coord_t obj2 );
        int monoCheckCollision( image_coord_t obj1, image_coord_t obj2 );
        
[   ...   ]

I shall try moving the monoCheckBoundingBox function above monoCheckCollision in the code itself.

Edit: moving monoCheckBoundingBox does solve that error, but surely by prototyping the functions seperately in the header, i no longer need to have consecutive functions in reverse used order(i.e. f2() above f1() if if1() uses f2())

Last edited by GalacticArachnid (2010-05-10 22:07:13)

Offline

#6 2010-05-11 00:11:23

scio
Member
From: Buffalo, NY
Registered: 2008-08-05
Posts: 366

Re: [Solved] Frustrating C++ error, possibly SDL

Hmm, if you have a header and a body separate, the order in the .cpp should not matter since the functions are declared in the .h.

Are you trying to do everything in 2 files, or do you have a .h and a .cpp for the class, and a separate .cpp for main?

If you don't have at least 3 files, I would suggest doing that for simplicity alone.  When you try to do too much in too few files, you run into things like severe order requirements.

Offline

#7 2010-05-11 07:13:40

toffyrn
Member
Registered: 2008-10-07
Posts: 221

Re: [Solved] Frustrating C++ error, possibly SDL

Could you post the entire files? (ie. via pastebin) smile

And i will try to help smile

Offline

#8 2010-05-11 09:57:04

GalacticArachnid
Member
Registered: 2009-01-02
Posts: 155
Website

Re: [Solved] Frustrating C++ error, possibly SDL

Thanks guys, ive uploaded the files to pastepin. here they are:

http://pastebin.com/PENtU994 game_objects.h
http://pastebin.com/Z5hnv0LH monolith.h
http://pastebin.com/h0Gbu0nG  monolith.cpp

I should also mention this is a WIP and im not sure about the separate .cpp for a class. Anyway, Ive looked at the code with a fresh mind and i think i will rewrite the monoLoadImage function to use a pointer to the SDL_image, so that it can also specify image_coord_t properties to the image at the same time. This may get rid of those errors.

However, it would still be nice to know what I was doing wrong here to learn from xD

Offline

#9 2010-05-11 11:31:28

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: [Solved] Frustrating C++ error, possibly SDL

You forgot to include your class on all your method declarations in the .cpp file

int monolith::monoInitVideo( 
SDL_Surface *monolith::monoLoadImage(
void monolith::monoPrepObject(
void monolith::monoDrawImage(
int monolith::monoCheckBoundingBox(
int monolith::monoCheckCollision(

Also, since this is C++, these defines are bad:

#define true 1
#define false 0

C++ supports a boolean datatype, you really shouldn't be redefining keywords like that.  Wherever you're using your current macros, change the datatype to 'bool' and get rid of those defines.

Also, from your int main, don't return 'true' or 'false', return actual integers (0, 1, 2, 3, etc...)

Offline

#10 2010-05-11 13:52:17

toffyrn
Member
Registered: 2008-10-07
Posts: 221

Re: [Solved] Frustrating C++ error, possibly SDL

Is it working better with cerebral's hints?

C/C++ is hard to learn compared to python/java etc, but it is much more fun wink

Offline

#11 2010-05-12 14:12:32

GalacticArachnid
Member
Registered: 2009-01-02
Posts: 155
Website

Re: [Solved] Frustrating C++ error, possibly SDL

Hi, thank you for that info - guess I didnt see some of my C habits creeping in xD

I have changed the code to include monolith:: before functions in the .cpp file and removed the true/false declarations.

Getting a whole bunch of errors about default values being given for arguments of monoInitVideo,

still have the same problem with monoLoadImage - if its not commented out in the header

and after moving monoCheckBounding box back below monoCheckCollision, im still being told that it has not been declared in the monolith class @.@

Also, sorry it took so long to reply, i ended up going out last night.. took me a while to wake up today ;D

Offline

#12 2010-05-12 14:39:40

scio
Member
From: Buffalo, NY
Registered: 2008-08-05
Posts: 366

Re: [Solved] Frustrating C++ error, possibly SDL

GalacticArachnid wrote:

Getting a whole bunch of errors about default values being given for arguments of monoInitVideo,

You cannot specify the default argument values in the .cpp, remove those and try again smile

Offline

#13 2010-05-12 14:52:34

GalacticArachnid
Member
Registered: 2009-01-02
Posts: 155
Website

Re: [Solved] Frustrating C++ error, possibly SDL

Ahh, okay, yeah that helped xD

However, Im still getting these two errors that I just cant figure out:

monolith.h:24: error: expected ';' before '(' token
monolith.cpp:37: error: no 'SDL_Surface* monolith::monoLoadImage(std::string, int&)' member function declared in class 'monolith'

Offline

#14 2010-05-12 15:43:02

scio
Member
From: Buffalo, NY
Registered: 2008-08-05
Posts: 366

Re: [Solved] Frustrating C++ error, possibly SDL

GalacticArachnid wrote:

However, Im still getting these two errors that I just cant figure out:

monolith.h:24: error: expected ';' before '(' token
monolith.cpp:37: error: no 'SDL_Surface* monolith::monoLoadImage(std::string, int&)' member function declared in class 'monolith'

For the first: Not sure if this will fix it, but you either need to create the function bodies, or remove the declaration since you don't really need it anyway (default constructor and destructor will be created for you).
For the second: Isn't monoLoadImage still commented out in the header? If not, please post updated code.

Last edited by scio (2010-05-12 15:43:42)

Offline

#15 2010-05-12 15:59:43

toffyrn
Member
Registered: 2008-10-07
Posts: 221

Re: [Solved] Frustrating C++ error, possibly SDL

The header file does not have "using namespace std" and therefor dont understand the "string" type smile

Try correct it to std::string... (Or even better: move "using namespace std" to the header.)

Edit: Also. it would be nice if you corrected the files on pastebin as you go, so it is easier to follow your process. (in case it does not work after this)

Last edited by toffyrn (2010-05-12 16:02:06)

Offline

#16 2010-05-12 16:14:28

scio
Member
From: Buffalo, NY
Registered: 2008-08-05
Posts: 366

Re: [Solved] Frustrating C++ error, possibly SDL

toffyrn wrote:

(Or even better: move "using namespace std" to the header.)

Do NOT do this, that is a horrible practice.  "using namespace" is fine in the body (.cpp) but can lead to all sorts of bad things in the header (.h) because it pollutes the namespace.

However, toffyrn is correct, in the header use "std::string" instead of "string" for monoLoadImage.

Offline

#17 2010-05-12 17:08:45

GalacticArachnid
Member
Registered: 2009-01-02
Posts: 155
Website

Re: [Solved] Frustrating C++ error, possibly SDL

Hi,

I have changed the line to std:string, thanks for pointing that one out xD Also, I have read somewhere before not to use namespace in the header - hence why it wasnt there before (I originally wanted to move it to the header when I was trying to solve this myself).

Also, pastebin files have been updated (I didnt realise I could do that wink)

Well, this got rid of those two errors! Sort of..

monolith.cpp:58: error: expected ';' before 'return'

now, this refers to the monoLoadImage function. the return statement in question is:

SDL_Surface *temp;
[   ...   ]
return temp;

No errors come up before that...

Offline

#18 2010-05-12 17:54:39

scio
Member
From: Buffalo, NY
Registered: 2008-08-05
Posts: 366

Re: [Solved] Frustrating C++ error, possibly SDL

GalacticArachnid wrote:
monolith.cpp:58: error: expected ';' before 'return'

This is where your

#define true 1

might be causing you problems.  Get rid of the #defines for true and false.

If you want to use words instead of values, use an enum (public or private depending on what you do with return values):

class monolith
{
public:
enum ReturnValues {
  Success = 0,
  Failure = 1
};
...
}

Then in your code body do:

return Success;

Instead of trying to return true defined as an int.

Offline

#19 2010-05-12 19:12:26

GalacticArachnid
Member
Registered: 2009-01-02
Posts: 155
Website

Re: [Solved] Frustrating C++ error, possibly SDL

scio, thank you for the suggestion, but I got rid of the #defines for true and false after reading Cerebral's comment up there xD

For the return statement in question, I am returning an SDL_Image, not an int wink

Last edited by GalacticArachnid (2010-05-12 19:13:34)

Offline

#20 2010-05-12 20:53:18

scio
Member
From: Buffalo, NY
Registered: 2008-08-05
Posts: 366

Re: [Solved] Frustrating C++ error, possibly SDL

Sorry, not the return

exitState = true;

exitState is an int and you have

return false;

in the version of monoLoadImage on pastebin.

Looks like your updates to the code didn't save, or they have a new address now, the links still show the #defines in there.

Offline

#21 2010-05-13 08:54:27

toffyrn
Member
Registered: 2008-10-07
Posts: 221

Re: [Solved] Frustrating C++ error, possibly SDL

Ok. Posting updated files:
monolith.h     http://pastebin.com/XjL2NAQf
monolith.cpp  http://pastebin.com/tpNNy2MH

And it compiles with

[toffyrn@hubble ~]$ g++ -Wall `sdl-config --cflags` `sdl-config --libs` -o monolith monolith.cpp -lSDL -lSDL_image
monolith.cpp: In function 'int main(int, char**)':
monolith.cpp:199:19: warning: unused variable 'surface'
monolith.cpp:199:28: warning: unused variable 'on_screen'

So it should at least compile, but as pointed out return true, and return false does not make a lot of sense...

PS: I did learn something too big_smile i will never more use namespace in my header...

Offline

#22 2010-05-13 11:34:31

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: [Solved] Frustrating C++ error, possibly SDL

toffyrn, your .cpp file is again missing the monolith:: in the method names.  The class never gets used. tongue

Galactic, I don't see the updates in your pastebin links included in this thread.  Can you re-upload if you're still having problems?

Offline

#23 2010-05-13 11:43:23

toffyrn
Member
Registered: 2008-10-07
Posts: 221

Re: [Solved] Frustrating C++ error, possibly SDL

Ah. shit. That wasnt even updated when started to edit! haha

Offline

#24 2010-05-13 12:10:25

GalacticArachnid
Member
Registered: 2009-01-02
Posts: 155
Website

Re: [Solved] Frustrating C++ error, possibly SDL

Damn, sorry guys. Ill update the files this evening - i have to make some food and run to labs right now xD

Okay, actually I do have time to update pastebin xD

Here are the pastebin files for progress so far, I dont know if url changed:

http://pastebin.com/taiuLR44 monolith.h
http://pastebin.com/FPg1rc2M monolith.cpp

game_objects.h has not been touched.

Also, thank you very much for all you feedback and help, I shall try the return true stuff this evening, but I really need to design this rc filter right now! ;D

Last edited by GalacticArachnid (2010-05-13 12:20:05)

Offline

#25 2010-05-13 13:30:52

toffyrn
Member
Registered: 2008-10-07
Posts: 221

Re: [Solved] Frustrating C++ error, possibly SDL

OK. changed some more things. Changes are marked as "//EDIT: a comment".

If i didn't do anything wrong (once more) it do compile at least...

http://pastebin.com/gVRLWWKB
http://pastebin.com/ujkW73rB

When it comes to your program's logic, and using SDL, i have no experience with that :S

Offline

Board footer

Powered by FluxBB