私はC++を学ぼうとしており、継承を解明しようとしているときにエラーが発生しました。コンパイル非常に基本的な継承:エラー: '{'トークンの前に期待されるクラス名
:daughter.cpp をファイルでは/home/jonas/kodning/testing/daughter.cpp:1からインクルード: /home/jonas/kodning/testing/daughter.h:6:エラー:期待クラス - を main.cppに:
#include "mother.h"
#include "daughter.h"
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" << endl;
mother mom;
mom.saywhat();
return 0;
}
mother.cpp:
ステータス1で終了 '{' トークン プロセス(0分0秒) 1エラー、0警告マイファイルの前に名前を付けます
#include "mother.h"
#include "daughter.h"
#include <iostream>
using namespace std;
mother::mother()
{
//ctor
}
void mother::saywhat() {
cout << "WHAAAAAAT" << endl;
}
mother.h:
#ifndef MOTHER_H
#define MOTHER_H
class mother
{
public:
mother();
void saywhat();
protected:
private:
};
#endif // MOTHER_H
daughter.h:
#ifndef DAUGHTER_H
#define DAUGHTER_H
class daughter: public mother
{
public:
daughter();
protected:
private:
};
#endif // DAUGHTER_H
とdaughter.cpp:私は何をしたいか
#include "daughter.h"
#include "mother.h"
#include <iostream>
using namespace std;
daughter::daughter()
{
//ctor
}
は娘がすべてを継承させることです母クラスの公衆(= saywhat())。私は間違って何をしていますか?
また、 'mother.h'または' mother.cpp'に 'daughter.h'を含める必要はありません。 あなたは相続財産をほぼ釘付けにし、提案された変更を行い、あなたは良いことをすべきです。 – nikhil
C + +のコンベンションのヒントは、あなたが学んでいるように、クラス名の最初の文字は大文字で始まります。これは要件ではありませんが、一貫したコーディング規則であることがわかります。また、以下のいくつかの回答に肯定的なコメントを残していることがわかります - あなたを最も助けてくれた回答を受け入れてください!各回答の横にチェックマークが表示され、それをクリックすると受け入れられます。 StackOverflowに貢献してくれてありがとう! – WendiKidd