我很新的C + +和書籍和YouTube和在這裏同時工作,但我有一個奇怪的問題,我找不到解決方案。 我試圖從一個基類「母親」繼承到一個派生類女兒,它不是爲我工作,它說它需要繼承在C++給予預期{上課
此代碼取自TheNewBoston,所以謝謝你Bucky,只是給學分
C:\CppProjects\inheritance\Daughter.h|6|error: expected class-name before '{' token
我添加的主要和類:
主:
#include <iostream>
#include "Mother.h"
#include "Daughter.h"
using namespace std;
int main()
{
Daughter t;
t.sayName();
}
媽媽級:
#include "Mother.h"
#include <iostream>
#include "Daughter.h"
using namespace std;
Mother::Mother()
{
}
void Mother::sayName(){
cout<<"I am !!!"<<endl;
}
母親頭:
#ifndef MOTHER_H
#define MOTHER_H
class Mother
{
public:
Mother();
void sayName();
};
#endif // MOTHER_H
子類:
#include "Daughter.h"
#include <iostream>
#include "Mother.h"
using namespace std;
Daughter::Daughter()
{
}
女兒頭:
#ifndef DAUGHTER_H
#define DAUGHTER_H
class Daughter : public Mother
{
public:
Daughter();
};
#endif // DAUGHTER_H
我看了大部分的搜索在這裏並沒有什麼發現,我甚至嘗試過noob風格,並添加std ::,它無關wi它。
謝謝大家願意嘗試的人!
只是讓你知道,當我試圖把所有的代碼都在一個文件中,它的偉大工程:
新的主:
#include <iostream>
using namespace std;
class Mother
{
public:
Mother();
void sayName(){
cout<<"I am!!!!"<<endl;
}
};
class Daughter: public Mother
{
public:
Daughter();
};
int main()
{
Daughter t;
t.sayName();
}
至少當包括女兒的頭部時,它不知道什麼叫母親 –
類可以請你進一步解釋?並解決它的方法? – shimon
想一下定義的作用。當編譯器啓動你的Daughter.cpp時,它看到了什麼?它看到一個鏈接到Daughter.h。有一堂課說「我是從母親那裏繼承的」,但是你沒有告訴編譯器母親是什麼。所以你必須首先有一個包含母體的定義。訂單很重要。 –