views:

105

answers:

2

Hey everybody, I need a bit of a hand with declaring a string array in my class header file in C++.

atm it looks like this:

//Maze.h
#include <string>

class Maze

{
    GLfloat mazeSize, mazeX, mazeY, mazeZ;
    string* mazeLayout;

public:
    Maze ( );
    void render();
};

and the constructor looks like this:

//Maze.cpp
#include <GL/gl.h>
#include "Maze.h"
#include <iostream>
#include <fstream>

Maze::Maze( )
{
    cin >> mazeSize;
    mazeLayout = new string[mazeSize];

    mazeX = 2/mazeSize;
    mazeY = 0.25;
    mazeZ = 2/mazeSize;
}

I'm getting a compiler error that says:

In file included from model-view.cpp:11:
Maze.h:14: error: ISO C++ forbids declaration of ‘string’ with no type
Maze.h:14: error: expected ‘;’ before ‘*’ token

and the only sense that makes to me is that for some reason it thinks I want string as a variable name not as a type declaration.

If anybody could help me out that would be fantastic, been looking this up for a while and its giving me the shits lol.

Cheers guys

+10  A: 

You need to qualify the name: std::string.

Apart from that, you class design doesn’t separate concerns properly: the class should not ask user input directly. That should be a parameter to the class constructor – the actual input handling should be outside the class.

Furthermore, I hope you’re properly deleting the memory you allocated in the class destructor. It would be better not to use a raw pointer. Use a std::vector<std::string> instead. This is much easier and safer.

Konrad Rudolph
A: 

Use C++ STL SGI.

Vector documentation: http://www.sgi.com/tech/stl/Vector.html

String documentation: http://www.sgi.com/tech/stl/basic_string.html

You access vector like as normal array because he have overloaded [] operator. But if you may add intems to a vector you must do it by the insert function, like that:

v.insert(v.end(), new_item);.

This code add a new_item at ends of the vector v.

Vector declaration you doing with std::vector<std::string>.

Svisstack
Why `v.insert(v.end(), new_item)`? http://stackoverflow.com/questions/2625006/2625223#2625223
sbi
thanks a lot, really helped me out
Dave