0

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'

我的StudFactory.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; 
}
4

1 回答 1

1

CreateStudent()你不能实例化一个MyBscImplementation对象,因为继承MyBscImplementationMyStudentImplementation但不实现纯虚函数void tell() const——它确实实现void tell()了,但这不是同一个函数签名。

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

于 2013-01-15T23:17:46.050 回答