错误:'class'未将类型命名为C++
Error: 'class' does not name a type C++
我看过很多关于这件事的帖子。但无论如何也解决不了我的问题。
我的问题是,我想从我的类B发送一个bang到我的类a。类a已经声明了类B (B B;),但不能在类B (a a;)中做同样的事情
可以看出,如果A类包含B类,那么如果B类包含A类,它就不起作用。"A不命名类型"
我将展示Metronome程序中的代码。
这是testApp.h
#ifndef TESTAPP_H_
#define TESTAPP_H_
#pragma once
#include "ofMain.h"
#include "ofxSimpleGuiToo.h"
#include "ofxSynth.h"
#include "threadedObject.h"
class testApp : public ofBaseApp{
public:
// virtual string getName() { return "testApp"; }
testApp(){
}
ofxSynth synth;
threadedObject TO;
void setup();
void update();
void draw();
int count;
int rate;
int last_time;
void audioOut(float *input, int bufferSize, int nChannels);
ofSoundStream soundstream;
//testApp();
friend void bang();
void keyPressed (int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
ofEventArgs eventA;
int mainAppsCount;
void bang(){
synth.trigger();
}
private:
int bpm;
int current_bpm;
float current_frequency;
float frequency;
float volume;
};
#endif
#ifndef THREADED_OBJECT_H
#define THREADED_OBJECT_H
#pragma once
#include "ofMain.h"
#include "testApp.h"
这里是threaddobject类!这里我只想发送一个"bang"到testApp
class threadedObject : public ofThread {
public:
testApp ta; #HERE IS THE FAIL
ofRectangle posRect;
int count;
int count2;
friend void bang();
double bpmToSeconds;
double changedBpm;
double sum;
int counter;
int check;
//--------------------------
threadedObject(){
// testApp hej;
count = 1;
count2=0;
bpmToSeconds=60;
changedBpm=135;
sum = bpmToSeconds/changedBpm;
counter=0;
}
//--------------------------
void start(){
startThread(true, false);
}
//--------------------------
void stop(){
stopThread();
}
//--------------------------
void threadedFunction(){
while( isThreadRunning() != 0 ){
if( lock() ){
count++;
if(count>4) count= 1;
unlock();
counter++;
ofSleepMillis(getBpm() * 1000);
}
}
}
//--------------------------
void bpm(double bpm)
{
changedBpm=bpm;
sum = bpmToSeconds/changedBpm;
}
//--------------------------
double getBpm(){
return sum;
}
//--------------------------
void draw(){
string str = "I am a slowly increasing thread. nmy current count is: ";
string tick = " TICK!";
string tock = " TOCK!";
posRect.set(150, 150, 0, 0);
if( lock() ){
str += ofToString(count);
if(counter%4==0){
str += tock;
}else {
str += tick;
#Here i want to send a bang to testApp like ta.bang();
}
unlock();
}else{
str = "can't lock!neither an errornor the thread has stopped";
}
ofDrawBitmapString(str, 350, 156);
}
int getTick()
{
return counter;
}
};
#endif
显示testApp.cpp
#include "testApp.h"
static const int bufferSize = 512;
static const int sampleRate = 44100;
//--------------------------------------------------------------
void testApp::setup(){
volume = 0.5f;
gui.addTitle("Master volume");
gui.addSlider("Volume", volume, 0.5f, 1.f);
frequency = current_frequency = 0.1f;
gui.addTitle("Base Frequency");
gui.addSlider("frequency", frequency, 20.f, 2000.f);
bpm=current_bpm;
bpm=135;
gui.addTitle("Metronome");
gui.addSlider("Bpm", bpm, 40.f, 140.f);
mainAppsCount = 0;
TO.start();
gui.show();
synth.setSampleRate(sampleRate);
soundstream.setup(this, 2, 0, sampleRate, bufferSize, 4);
}
//--------------------------------------------------------------
void testApp::update(){
if(current_frequency != frequency)
{
synth.setFrequency(frequency);
current_frequency = frequency;
}
synth.setVolume(volume);
mainAppsCount++;
TO.bpm(bpm);
}
//--------------------------------------------------------------
void testApp::draw(){
ofSetHexColor(0xffffff);
TO.draw();
string str = "I am a the main opengl thread.nmy current count is: ";
str += ofToString(mainAppsCount);
ofDrawBitmapString(str, 350, 256);
ofSetHexColor(0xff0033);
ofDrawBitmapString("press 's' to stop the thread and 'a' to start it", 250, 360);
gui.draw();
}
//--------------------------------------------------------------
void testApp::keyPressed(int key){
if (key == 'a'){
TO.start();
} else if (key == 's'){
TO.stop();
}
}
void testApp::audioOut(float * output, int bufferSize, int nChannels)
{
synth.audioOut(output, bufferSize, nChannels, 0);
}
//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
bang(); #The bang is in testApp.h. for just triggering the synth to play!
}
那么我如何使用我的bang()方法从testApp.h在threaddoobject .h?
A.h:
class B; // forward decl
class A {
B*b;
public:
void bang();
void send();
/* ... */
};
B.h:
class A; // forward decl
class B {
A*a;
public:
void bang();
void send();
/* ... */
};
A.cc
#include A.h
#include B.h
void A::bang() { /* ... */ }
void A::send()
{
b->bang();
}
B.cc
#include B.h
#include A.h
void B::bang() { /* ... */ }
void B::send()
{
a->bang();
}
当然,你可能只需要一个头文件和一个源文件。
编辑
当然,必须注意指针B::a
和A::b
指向有效的(完全构造的)对象。
相关文章:
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 检查 std::shared_ptr<> 的当前底层类型是否为 T
- 将结构字段的类型展开为可变模板参数
- 在Arduino中将PWM通道命名为LEDC是否有特定原因
- 将元组类型扩展为可变参数模板?
- 在 c++ 中将函数返回类型指定为模板参数
- 将复杂的非基元C++数据类型转换为 Erlang/Elixir 格式,以使用 NIF 导出方法
- 将类型声明为类型模板参数的模板参数的一部分是否合法?
- 当返回类型声明为 ListNode 时,我们是否可以返回 false<T>*
- 将类指针类型转换为键时出错
- 在 C++ 中将一个模板类型的对象类型转换为另一个模板类型
- 用于检测函数类型是否为否的特征
- 将函数参数类型声明为 auto
- 在将派生类指针类型转换为派生类指针后,从基类指针调用派生类函数
- 如何将Windows产品类型转换为名称?
- 在C++中将算术类型转换为 std::array 的最简单方法<uint8_t>
- 如何使用C++将.trc文件类型转换为文本文件?
- 从基本类型转换为非 POD 结构
- 错误:'class'未将类型命名为C++