下面根本无法编译,我无法修复它。希望一个好人能让我明白如何解决这个例子。
谢谢
我尝试编译:
# make
g++ -c -o client.o client.cpp
client.cpp: In function `int main()':
client.cpp:7: error: missing template arguments before "t"
client.cpp:7: error: expected `;' before "t"
client.cpp:8: error: `t' undeclared (first use this function)
client.cpp:8: error: (Each undeclared identifier is reported only once for each function it appears in.)
<builtin>: recipe for target `client.o' failed
make: *** [client.o] Error 1
client.cpp - 主要
#include<stdio.h>
#include"Test.h"
#include"Other.h"
int main() {
Test<Other> t = Test<Other>(&Other::printOther);
t.execute();
return 0;
}
测试.h
#ifndef TEST_H
#define TEST_H
#include"Other.h"
template<typename T> class Test {
public:
Test();
Test(void(T::*memfunc)());
void execute();
private:
void(T::*memfunc)(void*);
};
#endif
测试.cpp
#include<stdio.h>
#include"Test.h"
#include"Other.h"
Test::Test() {
}
Test::Test(void(T::*memfunc)()) {
this->memfunc= memfunc;
}
void Test::execute() {
Other other;
(other.*memfunc)();
}
其他.h
#ifndef OTHER_H
#define OTHER_H
class Other {
public:
Other();
void printOther();
};
#endif
其他.cpp
#include<stdio.h>
#include"Other.h"
Other::Other() {
}
void Other::printOther() {
printf("Other!!\n");
}
生成文件
all: main
main: client.o Test.o Other.o
g++ -o main $^
clean:
rm *.o
run:
./main.exe
Makefile 将允许轻松编译。