在C ++中出现错误之前无效使用了不完整的类型和预期的类型说明符

时间:2018-07-22 15:41:28

标签: c++ gcc

我正在以媒体播放器为例为状态设计模式写c ++代码。下面是代码。我得到无效使用不完整类型预期在类型错误之前。试图增加Class的正向减速,但不起作用。

#include<iostream>
using namespace std;
class MusicSystem;
class State {
public:
virtual bool playMusic(MusicSystem * pMusicSys);
virtual bool stopMusic(MusicSystem * pMusicSys);
virtual bool previousSong(MusicSystem * pMusicSys);
virtual bool nextSong(MusicSystem * pMusicSys);

};

bool State::playMusic(MusicSystem * pMusicSys) {
std::cout << "Sorry Music Cannot be played\n";
return false;
}
bool State::stopMusic(MusicSystem * pMusicSys) {
std::cout << "Sorry Music Cannot be played as Music is already stopped\n";
return false;
}
bool State::previousSong(MusicSystem * pMusicSys) {
std::cout << "Sorry previous song Cannot be played\n";
return false;
}
bool State::nextSong(MusicSystem * pMusicSys) {
std::cout << "Sorry previous song Cannot be played\n";
return false;
}


class StoppedState: public State {
public:
bool playMusic(MusicSystem * pMusicSys);

};
bool StoppedState::playMusic(MusicSystem * pMusicSys) {
pMusicSys->setCurrentState(new PlayingState());
std::cout << "Started Playing the music\n";
return true;
}

class PlayingState: public State {
public:
bool stopMusic(MusicSystem * pMusicSys);
bool previousSong(MusicSystem * pMusicSys);
bool nextSong(MusicSystem * pMusicSys);
};
bool PlayingState::stopMusic(MusicSystem * pMusicSys) {
pMusicSys->setCurrentState(new StoppedState());
std::cout << "Stopped Playing the music\n";
return true;
}
bool PlayingState::previousSong(MusicSystem * pMusicSys) {
std::cout << "Playing the previous song\n";
return true;
}
bool PlayingState::nextSong(MusicSystem * pMusicSys) {
std::cout << "Playing the next song\n";
return true;
}
class MusicSystem {
State * m_CurrentState;
public:
MusicSystem();
bool playMusic();
bool stopMusic();
bool previousSong();
bool nextSong();
void setCurrentState(State * currentState);
};
MusicSystem::MusicSystem() {
m_CurrentState = new StoppedState();
}
bool MusicSystem::playMusic() {
if (m_CurrentState)
return m_CurrentState->playMusic(this);
return false;
}
bool MusicSystem::stopMusic() {
if (m_CurrentState)
return m_CurrentState->stopMusic(this);
return false;
}
bool MusicSystem::previousSong() {
if (m_CurrentState)
return m_CurrentState->previousSong(this);
return false;
}
bool MusicSystem::nextSong() {
if (m_CurrentState)
return m_CurrentState->nextSong(this);
return false;
}

void MusicSystem::setCurrentState(State * currentState) {
if (m_CurrentState) {
delete m_CurrentState;
m_CurrentState = NULL;
}
m_CurrentState = currentState;
}



int main() {
MusicSystem obj;
obj.playMusic();
obj.nextSong();
obj.stopMusic();
obj.previousSong();
obj.playMusic();
return 0;
}

我收到以下错误

state pattern.cpp: In member function ‘virtual bool StoppedState::playMusic(MusicSystem*)’:
state pattern.cpp:37:14: error: invalid use of incomplete type ‘class MusicSystem’
pMusicSys->setCurrentState(new PlayingState());
^
state pattern.cpp:3:7: note: forward declaration of ‘class MusicSystem’
class MusicSystem;
^
state pattern.cpp:37:36: error: expected type-specifier before ‘PlayingState’
pMusicSys->setCurrentState(new PlayingState());
^
state pattern.cpp: In member function ‘virtual bool PlayingState::stopMusic(MusicSystem*)’:
state pattern.cpp:49:18: error: invalid use of incomplete type ‘class MusicSystem’
pMusicSys->setCurrentState(new StoppedState());
^
state pattern.cpp:3:7: note: forward declaration of ‘class MusicSystem’
class MusicSystem;

有人可以帮助我解决这些错误吗?

1 个答案:

答案 0 :(得分:1)

您的函数定义必须在某个位置,以便可以完全了解MusicSystem的完整定义,状态声明可以,但是成员定义则不能。

// header file
class MusicSystem;
class State {
...
};

// .cpp
#include "state.h"
#include "MusicSystem.h"

// member definitions here