tags:

views:

155

answers:

4

Encountered this problem before but forgot how I solved it.

I want to use the STL string class but the complier is complaining about not finding it. Here is the complete .h file.

#ifndef MODEL_H
#define MODEL_H

#include "../shared/gltools.h"  // OpenGL toolkit
#include <math.h>
#include <stdio.h>
#include <string>
#include <iostream>

#include "Types.h"

class Model
{

public:

    obj_type_ptr p_object;
    char Load3DS (char *p_filename);
    int LoadBitmap(char *filename);

    int num_texture;
    string fun("alex");

    Model(char* modelName, char* textureFileName);
};

#endif
+10  A: 

You want to be using std::string, yes?

You're just using string. Which works if you have a using namespace ... declaration, but isn't really a good idea in a header file.

Anon.
`::std::string`
Omnifarious
…and there are a couple ways to get it other than `using namespace`.
Potatoswatter
Anon, why it's not a good idea to use "using namespace" in a header file?
Lipis
@Lipis, Because every source file that includes the header will end up "using namespace" as well, which is usually not what you want - ie the namespace gets propagated throughout the project and ends up getting diluted value as a symbol-uniqueness-enhancer.
Justicle
So "using namespace" only inside the source files :) On the other hand though, according to many people it's not a really good practice to use them at all. Thanks Justicle
Lipis
@Lipis: Yeah, depends on how strict you are. Any time you have `using namespace xyz`, you risk name collisions. Some people think it's best to be strict and just never do `using namespace` at all. Some just avoid it at namespace scope (but allow it inside functions), and some allow it in .cpp files but not .h. I tend to follow the second approach (no `using namespace`s, except inside functions, where the effect is limited and doesn't pollute other code)
jalf
But it's not kinda annoying to write `std::` before `cout` all the time? And what about Potatoswatter's approach?
Lipis
@Lipis, I will do `using ::std::string` inside functions or in a .cpp file. I very rarely or never do `using namespace foo;`. Importing specific names is much nicer and has more predictable results than importing a whole namespace of names.
Omnifarious
+1  A: 

ohhh, std::string. Never use the using namespace in a header file btw.

Chris H
+1  A: 

Every identifier in the STL is in the std namespace. Until you do using namespace std;, using std::string;, or typedef std::string xxx;, it must be called std::string.

Any kind of using declaration in a header, especially outside your own namespace, is a bad idea, as others have mentioned.

So, import std::string into your class:

class Model
{
    typedef std::string string;

    public:
Potatoswatter
sigh… why the downvote?
Potatoswatter
+1  A: 

As well as the namespace issue mentioned by other answers, you can't construct a variable at its declaration as a class member. Assuming you changed it to

class Model {
    // ...
    std::string fun("alex");
};

This is still illegal inside a class, you cannot assign a value in the declaration, you have to leave it:

class Model {
    // ...
    std::string fun;
};

And if you want to give it "alex" when it is created, initialise it in the constructor:

Model::Model(...)
    : fun("alex")  // initialiser
{
    // ...
}
AshleysBrain
`std::string fun = "alex" is perfectly valid, and initializes the variable when it's declared. you can, and usually should, assign a value immediately when constructing a variable.
jalf
I was referring to when declaring it as a class member, because only static const integral data members can be initialised like that, which doesn't include string. Edited answer to clarify this.
AshleysBrain