クラス宣言の前にマクロを定義しています。マクロは、クラスのメンバー関数を呼び出します。私のサンプルコードは以下です。
サンプルクラス宣言、
// 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 ファイルでエラーが発生します。「サンプル」はクラスまたは名前空間の名前ではありません。
クラスのスコープ外でクラス宣言の前にクラスのメンバー関数を呼び出すにはどうすればよいですか? 私はプログラミングにまったく慣れていません。あなたのフィードバックは私を助けてくれます、ありがとう。