Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring a string array in class header file - compiler thinks string is variable name?

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

like image 401
Dave Avatar asked May 11 '26 05:05

Dave


1 Answers

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.

like image 156
Konrad Rudolph Avatar answered May 12 '26 19:05

Konrad Rudolph



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!