我正在类声明之前定义一个宏。该宏调用该类的成员函数。我的示例代码如下。在类声明之前调用一个类的成员函数
样品类的声明,
// sample.h
#include <sstream>
#include <iostream>
using namespace std;
#define CALCULATETEMP(a, b, c) {
int d = Sample::getTempIncrement(a,b,c);
stringstream ss;
ss << d;
cout << ss.str() << endl;
}
class Sample {
public:
Sample();
int getTempIncrement(int a, int b, int c);
~Sample();
};
样品类的实现,
//sample.cpp
#include "sample.h"
Sample::Sample() {
}
int Sample::getTempIncrement(int a, int b, int c) {
int temp = 5;
int d = (a*temp) + (b+c)*temp;
return d;
}
Sample::~Sample() {
}
主程序,
//main.cpp
#include "sample.h"
int main(int argc, char* argv[]) {
int a = 1;
int b = 2;
int c = 3;
CALCULATETEMP(a, b, c);
return 0;
}
当我运行main.cpp中,我得到一个错误宏定义中的sample.h文件:“Sample”不是类或名称空间名称。
如何在类的范围之外和类声明之前调用类的成员函数?我对编程颇为陌生,您的反馈会帮助我,谢谢。
是的,'stringstream'在那里有一些开销,谢谢。我只是在尝试宏,没有具体说明不用函数做它。 –
@capricorn_heitus好吧,试验没有错:)我只是想指出它的情况下,这是真正的代码的地方。 –
请记住,不要在'\'之后留下任何空格,否则最终会出现同样的问题。它将逃离空间而不是任何换行。 –