Parts of C++ not linking properly?

Jones
Unregistered
 
Post: #1
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:

Code:
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:

Code:
#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? Huh

Thanks!
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
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.
Quote this message in a reply
Member
Posts: 254
Joined: 2005.10
Post: #3
Why is "using namespace std;" bad style?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #4
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 Smile
Quote this message in a reply
Jones
Unregistered
 
Post: #5
OneSadCookie Wrote: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:

Code:
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.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #6
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
Quote this message in a reply
Jones
Unregistered
 
Post: #7
Ah, then I'm using it wrong. Let's say this is my struct:

Code:
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? Smile

Thanks!
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #8
* 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.
Quote this message in a reply
Jones
Unregistered
 
Post: #9
OneSadCookie Wrote:* 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 Rasp Wow Sad

*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).
Quote this message in a reply
Member
Posts: 254
Joined: 2005.10
Post: #10
If you want to send a message to the pointer in your struct you should use:
Code:
myFoo.surf->message();
Quote this message in a reply
Member
Posts: 254
Joined: 2005.10
Post: #11
Jones Wrote: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.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #12
Jones Wrote:So along that logic... it is impossible to put an SDL_surf in a struct...

Blink

You should work on your reading comprehension Rolleyes
Quote this message in a reply
Moderator
Posts: 771
Joined: 2003.04
Post: #13
Jones Wrote:Ah, then I'm using it wrong. Let's say this is my struct:

Code:
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? Smile

Thanks!

Neither Wink

What you want is just:
Code:
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:
Code:
(*myFoo.surf)
but as I mentioned, SDL functions actually expect a pointer anyway, so there should be no need for that.
Quote this message in a reply
Jones
Unregistered
 
Post: #14
Well, then it's like any pointer in a function call... like this, n'est-ce-pas?

Code:
myFoo.&surf

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

Thanks!
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #15
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.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Gett my Quaternions into my matrix properly johnboy007 1 2,510 Feb 21, 2007 01:44 PM
Last Post: OneSadCookie
  dynamic array not working properly wyrmmage 4 3,430 Dec 8, 2006 03:22 PM
Last Post: wyrmmage
  Updating only parts of the screen Josh 1 2,983 Dec 18, 2002 02:38 AM
Last Post: kelvin