我有一个Note
和一个Track
班级,两者都是*generator
成员。当我创建新Note
对象时,我想将成员链接到的generator
成员Note
,Track
但我不知道如何做到这一点。
#include <iostream>
using namespace std;
class Generator {
public:
virtual float getSample(int sample)=0;
};
class Note {
public:
Generator *generator; // THIS IS WHAT IS CAUSING ME TROUBLE
Note(Generator *aGen){
generator = aGen;
}
};
class Synth : public Generator{
public:
virtual float getSample(int sample);
int varA;
int varB;
Synth(){
varA = 5;
varB = 8;
}
};
float Synth::getSample(int sample){
varA = sample;
varB = 3;
return 0;
}
class Track {
public:
Generator *generator;
Track(){
generator = new Synth();
}
};
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
Track track = Track();
cout << "test" << endl;
return 0;
}
我想过做这样的事情,但它不起作用:
Track track = Track();
Note n = Note(&track.generator);
错误
prog.cpp: In function ‘int main()’:
prog.cpp:48:35: error: no matching function for call to ‘Note::Note(Generator**)’
prog.cpp:48:35: note: candidates are:
prog.cpp:13:5: note: Note::Note(Generator*)
prog.cpp:13:5: note: no known conversion for argument 1 from ‘Generator**’ to ‘Generator*’ prog.cpp:9:7: note: Note::Note(const Note&)
prog.cpp:9:7: note: no known conversion for argument 1 from ‘Generator**’ to ‘const Note&’ prog.cpp:48:10: warning: unused variable ‘n’ [-Wunused-variable] - See more at: http://ideone.com/E38ibe#sthash.V3QMcYJQ.dpuf