简单多态性投射不起作用

Simple Polymorphism cast not working

本文关键字:不起作用 多态性 简单      更新时间:2023-10-16

我有一个类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;

它不允许我说"periodicblabla类型的值不能分配给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时,它说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保护?

没有它们可能会导致你所看到的各种问题。

三种猜测:

  • 您在头文件之间复制并粘贴了代码,却忘记更改#include保护
  • 您使用预编译的头,并在#include"stdafx.h"之前包含一些内容
  • 如果使用预编译的标头,请尝试删除.pch文件