找不到我的错误 - 抽象错误

时间:2013-01-15 23:08:20

标签: c++ design-patterns abstract-class

4小时的痛苦 - 尝试了一切......帮助!

第一次尝试使用设计模式,并且不知道我做错了什么......

以下是一些代码。我得到的错误是:

Error   1   error C2259: 'MyBscImplementation' : cannot instantiate abstract class  
Error   3   error C2259: 'MyBscImplementation' : cannot instantiate abstract class
Error   5   error C2259: 'MyBscImplementation' : cannot instantiate abstract class
Error   2   error C2248: 'MyPhdImplementation::MyPhdImplementation' : cannot access private member declared in class 'MyPhdImplementation'  
Error   4   error C2248: 'MyPhdImplementation::MyPhdImplementation' : cannot access private member declared in class 'MyPhdImplementation'  
Error   6   error C2248: 'MyPhdImplementation::MyPhdImplementation' : cannot access private member declared in class 'MyPhdImplementation'

myStudFactory.h:

#ifndef __MYSTUDFACTORY_H
#define __MYSTUDFACTORY_H

#include <iostream>
using namespace std;
#include <string>

#include "myStudImpl.h"
#include "myBscImpl.h"
#include "myMscImpl.h"
#include "myPhdImpl.h"

class MyStudentFactory
{
public:
    static MyStudentImplementation* CreateStudent(const string& studentType, string name, int id, MySubj* s)
    {
        MyStudentImplementation* ptrStudent = 0;
        if(studentType == "B.Sc")
            ptrStudent = new MyBscImplementation(s, name, id);
        if(studentType == "M.Sc")
            ptrStudent = new MyMscImplementation(s, name, id);
        if(studentType == "Ph.D")
            ptrStudent = new MyPhdImplementation(s, name, id);
        return ptrStudent; ///////////////////////!!!!!
        //*%^$&^$@#%//
    }
private:
    MyStudentFactory();
    ~MyStudentFactory();
};


#endif

myStudImpl.h:

#ifndef __MYSTUDIMPL_H
#define __MYSTUDIMPL_H

#include <iostream>
using namespace std;
#include <string>

class MyStudentImplementation
{
public:
    MyStudentImplementation(string name="NULL", int id=0);
    virtual void tell() const = 0;
    string getName() const {return studentName;}
    int getId() const {return studentId;}

protected:
    string studentName;
    int studentId;
};


#endif

myExamObs.h:

#ifndef __MYEXAMOBS_H
#define __MYEXAMOBS_H

#include <iostream>
using namespace std;
#include <string>
#include "myDate.h"

class MySubj;

class MyExamObs
{
public:
    virtual ~MyExamObs() {}
    virtual void Update(MySubj* ChangeSubject, const myDate& exam_date) const = 0;
protected:
    MyExamObs() {}
    MySubj* sbj;
};

#endif

myExamObs.cpp:

#include "myExamObs.h"
#include "myBscImpl.h"
#include "mySubj.h"

MyBscImplementation::MyBscImplementation(MySubj* s, string name, int id) : MyStudentImplementation(name, id)
{
    sbj = s;
    sbj->Attach(this);
}

MyBscImplementation::~MyBscImplementation()
{
    sbj->Detach(this);
}

void MyBscImplementation::Update(MySubj* ChangeSubject, const myDate& exam_date)
{
    if (ChangeSubject == sbj)
    {
        cout << studentName + " had been informed that the exam was postponed to: "; 
        exam_date.print();
    }
}

myBscImpl.h:

#ifndef __MYBSCIMPL_H
#define __MYBSCIMPL_H

#include <iostream>
using namespace std;
#include <string>

#include "myExamObs.h"
#include "myStudImpl.h"
#include "myDate.h"

class MyBscImplementation : public MyStudentImplementation, public MyExamObs
{
public:
    MyBscImplementation(string name, int id);
    void tell();
    MyBscImplementation(MySubj* s, string name, int id);
    ~MyBscImplementation();
    void Update(MySubj* ChangeSubject, const myDate& exam_date);
};

#endif

myBscImpl.cpp:

#include "myBscImpl.h"

MyBscImplementation::MyBscImplementation( string name, int id ) : MyStudentImplementation(name, id)
{

}

void MyBscImplementation::tell() 
{
    cout << "BScstudent name is:" << studentName <<", Id is: " << studentId << endl; 
}

1 个答案:

答案 0 :(得分:1)

CreateStudent()中,您无法实例化MyBscImplementation对象,因为MyBscImplementation继承自MyStudentImplementation但未实现纯虚函数void tell() const - 它确实实现void tell(),但这不是相同的函数签名。

你还没有给出MyPhdImplementation的代码,所以我不能告诉你它有什么问题。