c++ - 简单多态转换不起作用

标签 c++ oop class inheritance polymorphism

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

以及两个类的实现:

源代码.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 的基类)

和错误消息:无法将“PeriodicSourceComponent*”类型的值分配给“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 的成员,这就是我使用强制转换的原因首先..

最后,这是组件类,项目中几乎所有其他类的主类: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/

相关文章:

java - 类结构应该匹配 OOP 中的表结构吗?

c# - 什么是阅读 ISomething : ISomethingElse 的正确方法

matlab - 在 Matlab 中的父类(super class)上定义自定义方法和属性 block

c++ - 指针未指向 NULL 时发生访问冲突

c++ - 使用 std::thread 和良好实践并行化循环

c++ - 如何成功创建一个创建对象并返回指向该对象的指针的函数?

java - 找不到符号 = 新

c++ - 为什么这个 EXC_BAD_ACCESS 发生在 long long 而不是 int 上?

PHP 对象扩展 $this->some_object->method

class - 拆分 UML 类图?