Parts of C++ not linking properly?
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:
Now, I don't get any errors directly from the include, which I do like this:
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!
Code:
flx_spriteEffect.h:24: error: 'ifstream' does not name a type
flx_spriteEffect.h:75: error: 'string' was not declared in this scopeNow, 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?
Thanks!
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.
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.
Why is "using namespace std;" bad style?
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
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
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.
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
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
Ah, then I'm using it wrong. Let's say this is my struct:
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!
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?

Thanks!
* 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.
a.b means "the component of a named b", so b can never contain any operators.
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...
*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).
If you want to send a message to the pointer in your struct you should use:
Code:
myFoo.surf->message();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.
Jones Wrote:So along that logic... it is impossible to put an SDL_surf in a struct...
You should work on your reading comprehension
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?
Thanks!
Neither

What you want is just:
Code:
myFoo.surfThat 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)
Well, then it's like any pointer in a function call... like this, n'est-ce-pas?
I'll try that, and just plain old myFoo.surf
Thanks!
Code:
myFoo.&surfI'll try that, and just plain old myFoo.surf
Thanks!
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.
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.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Gett my Quaternions into my matrix properly | johnboy007 | 1 | 2,249 |
Feb 21, 2007 01:44 PM Last Post: OneSadCookie |
|
| dynamic array not working properly | wyrmmage | 4 | 3,074 |
Dec 8, 2006 03:22 PM Last Post: wyrmmage |
|
| Updating only parts of the screen | Josh | 1 | 2,739 |
Dec 18, 2002 02:38 AM Last Post: kelvin |
|

