PDA

View Full Version : Parts of C++ not linking properly?


Jones
2006.06.21, 07:18 PM
I've got an SDL application I'm trying to compile to test select parts of a project I've been working on. Problem is, I don't think it's linking normally to parts of the c++ library. Specifically, this is the error that I mean:

flx_spriteEffect.h:24: error: 'ifstream' does not name a type
flx_spriteEffect.h:75: error: 'string' was not declared in this scope


Now, I don't get any errors directly from the include, which I do like this:


#include <fstream>
#include "string.h"
// Also tried like: <string.h>


My extensions are all .cpp or .h, and I've tried forcing c++ with the "-x c++" argument to the gcc command line.

No luck, what's up? :???:

Thanks!

OneSadCookie
2006.06.21, 07:30 PM
1) those types are in the std namespace; and should be refered to as std::ifstream, std::string, etc.

2) you can, if you insist, put "using namespace std;" after your includes to allow you to omit the std:: prefix, but this is bad style.

3) <string.h> is the C header file that declares strcpy and friends. <cstring> is that same header file, C++ized, and with the functions in namespace std. <string> is the header that defines the C++ string class std::string.

Blacktiger
2006.06.21, 08:06 PM
Why is "using namespace std;" bad style?

OneSadCookie
2006.06.21, 08:39 PM
Because the point of namespaces is so names don't clash between programs and libraries made by different people (eg. you can make a class called "vector" without worrying about the STL already having one, and the STL can add new names in the future without worrying about clashing with every single piece of third-party code in existence). using namespace std bypasses that safeguard.

std:: is not much to type, and it's not something you have to type often. It's worth doing it so your code doesn't break in the future :)

Jones
2006.06.21, 08:50 PM
1) those types are in the std namespace; and should be refered to as std::ifstream, std::string, etc.

2) you can, if you insist, put "using namespace std;" after your includes to allow you to omit the std:: prefix, but this is bad style.

3) <string.h> is the C header file that declares strcpy and friends. <cstring> is that same header file, C++ized, and with the functions in namespace std. <string> is the header that defines the C++ string class std::string.

Doh! Hehe... silly mistake.

Well, that sped things up. But I have new question... (don't I always?)

When I have a pointer in a struct, I know that to access it I need the -> operand, but in some cases it produces an error such as this:

flx_spriteEffect.h:100: error: base operand of '->' has non-pointer type 'FLX_spriteEffectType'


The struct itself is not a pointer, but the item inside it, which incidentally is a struct itself (SDL_Surface), is.

What could be wrong?

Thanks.

OneSadCookie
2006.06.21, 09:46 PM
When you have a pointer to a struct, you use pointer->field to get at the internals.
When you have a struct, you use struct.field to get at the internals.

The rules apply transitively (that is, if struct field "field0" contains a pointer to another struct, you'd do pointer->field0->field1 or struct.field0->field1; if struct field "field0" is another struct, you'd do pointer->field0.field1 or struct.field0.field1).

Another way of saying it: foo->bar is just shorthand for (*foo).bar

Jones
2006.06.22, 12:05 AM
Ah, then I'm using it wrong. Let's say this is my struct:


typedef struct {
SDL_Surface *surf;
} foo;

foo myFoo;



How do I access the pointer in the struct? Like this:

myFoo.*surf;

or like this:

myFoo->surf

Currently, the second does not work, and I don't think the first is a legal expression.

So, what's the right syntax? :)

Thanks!

OneSadCookie
2006.06.22, 12:12 AM
* means "dereference a pointer". That's not what you mean here.
a.b means "the component of a named b", so b can never contain any operators.

Jones
2006.06.22, 01:29 AM
* means "dereference a pointer". That's not what you mean here.
a.b means "the component of a named b", so b can never contain any operators.

So along that logic... it is impossible to put an SDL_surf in a struct...

:cry: :lol: :zzz: :p :wow: :(

*smashes head on desk*

Well... nothing will work now. *Laughs like a crazy demented man*

Actually this comes as a huge relief. I can lower my sights and concentrate on a smaller project now. But I have the strangest feeling this problem will return. It's my addiction... I need to go to SUA (Struct Users Anonymous).

Blacktiger
2006.06.22, 01:31 AM
If you want to send a message to the pointer in your struct you should use:

myFoo.surf->message();

Blacktiger
2006.06.22, 01:41 AM
It's my addiction... I need to go to SUA (Struct Users Anonymous).
I think that there is an appropriate time/place to use structs. A struct is just a way to create a set of data that has no associated operations and whose members are public. Sometimes using a class is overkill.

That being said, OOP is there to help you and not taking advantage of its features such as data-hiding and polymorphism is not good either.

OneSadCookie
2006.06.22, 01:44 AM
So along that logic... it is impossible to put an SDL_surf in a struct...

:blink:

You should work on your reading comprehension :rolleyes:

PowerMacX
2006.06.22, 03:25 AM
Ah, then I'm using it wrong. Let's say this is my struct:


typedef struct {
SDL_Surface *surf;
} foo;

foo myFoo;



How do I access the pointer in the struct? Like this:

myFoo.*surf;

or like this:

myFoo->surf

Currently, the second does not work, and I don't think the first is a legal expression.

So, what's the right syntax? :)

Thanks!

Neither ;)

What you want is just:
myFoo.surf

That gives you the pointer, which is what all SDL functions would ever need. If for some reason you want the content of the surf pointer, then you would use this:
(*myFoo.surf)
but as I mentioned, SDL functions actually expect a pointer anyway, so there should be no need for that.

Jones
2006.06.22, 01:29 PM
Well, then it's like any pointer in a function call... like this, n'est-ce-pas?


myFoo.&surf


I'll try that, and just plain old myFoo.surf

Thanks!

ThemsAllTook
2006.06.22, 02:09 PM
Accessing a field in a struct:
struct.field

Accessing a field in a pointer to a struct:
struct->field
OR:
(*struct).field

Accessing the contents of a pointer in a struct:
*struct.field
OR:
*(struct.field)

Accessing the contents of a pointer in a pointer to a struct:
*struct->field
OR:
*(*struct).field
OR:
*(struct->field)
OR:
*((*struct).field)

Hope this helps.

Jones
2006.06.22, 03:12 PM
Well it's working now, but for a couple of SIGBUS (sig 10) errors, but I think I'll take a break from this particularly problematic code during exam week. Maybe something simpler for a while.