PeriodicSourceComponent

PeriodicSourceComponent

我有一个类SourceComponent及其派生类PeriodicSourceComponent
实现为:

class SourceComponent : public Component
{
protected:
    friend class UserInterface;
    friend void readInput();
public:
    virtual int returnType();
    virtual int propagateLogic();
    virtual void sourcePropagation(float);

    virtual void accept(VisitorSources&);
    SourceComponent();
};




 #include "source_component.h"

class PeriodicSourceComponent : public SourceComponent
{
private:
    int frequency;
    friend void main();
    friend void readInput();
    friend class UserInterface;
public:
    void sourcePropagation(float);
    int returnType();
    PeriodicSourceComponent();
};


当我尝试不同的课程/方法来做时:

SourceComponent* s = new PeriodicSourceComponent;


它不会让我说,“不能将periodblabla类型的值分配给sourceblabla类型的值”。为什么?

编辑:

好的,在我看来,这看起来像这样:

#include "source_component.h"
#include "periodic_source_component.h"
void main()
{
     SourceComponent* s = new PeriodicSourceComponent;
}


以及这两个类的实现:

source.cpp:

 #include "source_component.h"

    SourceComponent::SourceComponent()
    {
         outputState = -1;
    }

    int SourceComponent::propagateLogic()
    {
        return 1;
    }

    int SourceComponent::returnType()
    {
        return 5;
    }


和periodic.cpp

#include "periodic_source_component.h"

PeriodicSourceComponent::PeriodicSourceComponent()
{
    outputState = 0;

}

int PeriodicSourceComponent::returnType()
{
    return 3;
}

void PeriodicSourceComponent::sourcePropagation(float time)
{
    float t = time, period;
    period = 1000000/frequency;
    if(t > period)
    {
        while(t >= period)
            t -= period;
    }
    if(t <= (period/2))
            outputState = 0;
        else
            outputState = 1;

}


并且它不起作用...(outputState是Component类的成员,SourceComponent的基类)

和错误消息:A value of type "PeriodicSourceComponent*" cannot be assigned to a value of type "SourceComponent*".

重要编辑
当我尝试编译时,实际的编译器错误是在PeriodicSourceComponent声明中,它说:“基类未定义”。

而且,我还有其他两个SourceComponent派生的类,但是我看不到它们如何干扰这一类。

编辑4
好的,所以我找出了导致错误的原因,您当然是对的,我没有发布过其他内容。我有VisitorSources类,这里是定义:

#ifndef __VISITOR_SOURCES_H__
#define __VISITOR_SOURCES_H__

#include "component.h"
#include "impulse_source_component.h"
#include "arbitrary_source_component.h"
#include "periodic_source_component.h"

class VisitorSources
{
protected:
    VisitorSources();

public:
    virtual void visitImpulseSource(ImpulseSourceComponent*);
    virtual void visitArbitrarySource(ArbitrarySourceComponent*);
    virtual void visitPeriodicSource(PeriodicSourceComponent*);
    void visitSource(int, float);
};


#endif


其实现尚未编写:

#include "visitor_sources.h"

void visitSource(int type, float time)
{
}


void VisitorSources::visitArbitrarySource(ArbitrarySourceComponent* a)
{
}


当我注释掉整个Visitor类和实现时,上述错误由于某种原因而消失了。我不知道为什么...

剩下的唯一错误是,当我尝试使用s-> frequency时,它说频率不是SourceComponent的成员,这是事实,但它是PeriodicSourceComponent的成员,这就是为什么我在第一个中使用强制类型转换地点..

最后,这是Component类,它是项目中几乎所有其他类的主类:P

#ifndef __COMPONENT_H__
#define __COMPONENT_H__

#include <iostream>
#include <vector>

class Component
{
    friend class UserInterface;
    friend void readAndSimulate();
protected:
    Component();
    int numOfInputs;
    int numOfOutputs;
    std::vector<Component*> inputs;
    std::vector<Component*> outputs;
    friend void main();
    float lengthOfSimulation;
    int typeIfSource;


public:
    int outputState;
    virtual int propagateLogic() = 0;
    virtual int returnType();

    int beginPropagation();
    virtual void sourcePropagation(float);

    ~Component();


};

#endif


并实现:

#include "component.h"
#include <conio.h>


Component::Component()
{
}

int Component::beginPropagation()
{
    std::vector<Component*>::const_iterator iter = outputs.begin();

    for(;iter<outputs.end();++iter)
    {
        if ((*iter)->outputState == -2)
        {
            (*iter)->outputState = outputState;
            return (*iter)->outputState;
        }
    }

    std::vector<Component*>::const_iterator it = outputs.begin();
    int finishedCycle, x;
    while(1)
    {
        finishedCycle = 1;
        for(; it < outputs.end(); ++it)
        {
            x = (*it)->propagateLogic();
            if(!x)
                finishedCycle = 0;
        }
        if(finishedCycle) break;
        it = outputs.begin();
    }
    it = outputs.begin();
    for(;it<outputs.end();++it)
        (*it)->beginPropagation();
}


int Component::returnType()
{
    return 0;
}

void Component::sourcePropagation(float)
{
}


Component::~Component()
{
    std::vector<Component*>::const_iterator it = inputs.begin();
    for(; it < inputs.end(); ++it)
    {
        if((*it) != NULL)
        {
            delete *it;
            Component* p = *it;
            p = NULL;
        }
    }
    it = outputs.begin();
    for(; it < inputs.end(); ++it)
    {
        if((*it) != NULL)
        {
            delete *it;
            Component* p = *it;
            p = NULL;
        }
    }
}

最佳答案

您是否在所有头文件中使用include guards

没有它们会导致您遇到的各种问题。

关于c++ - 简单多态转换无效,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9173975/

10-15 18:06