は、次のヘッダーファイルを++ C++ではなぜint変数はクラスのprivateデータメンバとして宣言できますが、string変数では宣言できませんか? Cで
#ifndef SAMPLE_H_
#define SAMPLE_H_
class Sample {
private:
int number;
};
#endif
しかし、次のヘッダーファイル
合法で違法である#ifndef
#define
class Sample {
private:
string name;
};
#endif
なぜそれがそのようなものですか?私の場合は
I次のヘッダファイルがあります。
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のターミナルコマンドを実行します。この後
g++ *.cpp
./a.out
私は、次のエラーを取得しています:
エラー:
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;
私はprivate
としてheader file
でstring member variable
を宣言しようとしています、 constructor
は、両方が有効です。ヘッダファイルに '#include'がありませんので、エラーが発生していると思います。 –
Ivan
私たちができることがあればどんなエラー(もしあれば)もわからなくても推測できます。 [良い質問をする方法を読む](http://stackoverflow.com/help/how-to-ask)を読んで、[最小限の完全で検証可能な例](http:// stackoverflow。 com/help/mcve)。 –
あなたが表示するコードスニペットは実際には無効です。 '#ifndef'と' #define' *何*? –