-3

In C++ the following header file is legal

#ifndef    SAMPLE_H_
#define    SAMPLE_H_

class Sample {
    private:
        int number;
};

#endif

But the following header file is illegal

#ifndef
#define

class Sample {
    private:
        string name;
};

#endif

Why is it like that?

In my case I have the following header file:

Alphabet.h

#include <string>

#ifndef         ALPHABET_H_
#define         ALPHABET_H_


class Rhyme {

private:
    string a;

public:

    Rhyme ();

};

#endif

Alphabet.cpp

#include <iostream>
#include "Alphabet.h"

using namespace std;

Rhyme::Rhyme () {

    a = "A for Apple";
}

Main.cpp

#include <iostream>
#include "Alphabet.h"

using namespace std;

int main () {

    Rhyme rhyme;
    return 0;
}

Linux terminal command:

g++ *.cpp
./a.out

After this I am getting the following error:

Error:

    In file included from Alphabets.cpp:2:0:
Alphabet.h:10:2: error: ‘string’ does not name a type
  string a;
  ^
Alphabets.cpp: In constructor ‘Rhyme::Rhyme()’:
Alphabets.cpp:8:2: error: ‘a’ was not declared in this scope
  a = "A for Apple";
  ^
In file included from Main.cpp:2:0:
Alphabet.h:10:2: error: ‘string’ does not name a type
  string a;

I am trying to declare a string member variable in header file as private, and then initialize it from another file using constructor

Histar
  • 25
  • 3
  • 6
    Both are legal. I think you are getting an error because you are missing `#include ` in your header file. –  Nov 18 '17 at 23:26
  • 3
    Without knowing what errors (if any) you get all we can do is guess. Please [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask), and learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Nov 18 '17 at 23:27
  • 1
    Oh and the code snippets you show are really not valid, any of them. `#ifndef` and `#define` *what*? – Some programmer dude Nov 18 '17 at 23:28
  • It's always a good idea to post the concrete error message you are seeing. I guess that @Ivan is right about the missing include. – Jens Nov 18 '17 at 23:28
  • 1
    Because 'std::string' is not a primitive type. You need the header. – Keith Nov 18 '17 at 23:29
  • you should include related header of string which is `#include ` and also declare it as `std::string` or add `using namespace std;` statement before class declaration – Onur A. Nov 18 '17 at 23:29
  • 3
    @OnurA. Note that [`using namespace std;` is not good practice](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). Especially in header files. – Some programmer dude Nov 18 '17 at 23:32
  • @Someprogrammerdude I agree, I just offered alternative solutions to his problem ;) – Onur A. Nov 18 '17 at 23:35
  • @Someprogrammerdude updated my answer – Histar Nov 18 '17 at 23:53

2 Answers2

4

In C++, int is a builtin keyword and is a valid type anywhere in the code. string is a class in the std namespace defined in the <string> header and may only be used if you include the header first.

You should not use the using namespace directive in header files (namespace pollution), so you need to write std::string.

Also, use the file name of your header (e.g. SAMPLE_H) for include guard:

#ifndef SAMPLE_H
#define SAMPLE_H

#include <string>

class Sample {
    private:
        std::string name;
};

#endif
r0the
  • 617
  • 4
  • 13
1

std::string is a standard user-defined class that is declared in the header <string>. So you need to include the header

#include <string>

and is placed in the standard name space std.

So you need at least write

class Sample {
    private:
        std::string name;
};
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335