bobber205
bobber205

Reputation: 13372

Not Able To Use STL's string class

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

Upvotes: 1

Views: 351

Answers (4)

AshleysBrain
AshleysBrain

Reputation: 22591

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
{
    // ...
}

Upvotes: 1

Potatoswatter
Potatoswatter

Reputation: 137770

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:

Upvotes: 3

Chris H
Chris H

Reputation: 6581

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

Upvotes: 1

Anon.
Anon.

Reputation: 60008

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.

Upvotes: 11

Related Questions