简单多态性投射不起作用
Simple Polymorphism cast not working
我有一个类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文件
- C++17 多态记忆资源不起作用
- C++ 多态性在代码::块 17.12 中不起作用
- 无法初始化已知大小的矢量指针,该大小不会因多态性而更改
- 任务计划程序库的预处理不起作用 - 多定义错误
- C++矢量 - 放置/擦除不起作用?(多态性)
- 使用字符串时不能重写多态性
- 多态性在 lambda 函数C++不起作用
- C++多态性为什么这不起作用?
- C++多态性/虚拟函数在此不起作用
- 多态性测试不起作用
- 如果重载函数仅被部分重写,则多态性不起作用
- 为什么说C++不支持参数多态性
- 为什么这C++多态性不起作用
- 为什么AbstractFactoryUnit具有动态多态性而不是静态多态性
- 异常处理+多态性,如果异常方法在同一类中不起作用,则不起作用
- 简单多态性投射不起作用
- 为什么不能将多态性与值一起使用,而是引用和指针
- 模板化多态性不起作用
- 两级层次类中的C++多态性:不明白为什么它不起作用
- 为什么多态性在这种情况下不起作用