Mình có một class Fraction. Khai báo trong file .h như sau. Hàm getNumberOfFraction() dùng để đếm số lượng đối tượng đã được tạo ra trong hàm main.
Fraction.h
class Fraction
{
static int countFraction;
static int Denom;
static int Numer;
public:
static int getNumberOfFraction();
static void setDataToFraction(int _denom, int _numer);
static int GetNextID() { return countFraction++; }
Fraction()
{
countFraction++;
}
};
Fraction.cpp
void Fraction::setDataToFraction(int _denom, int _numer)
{
Denom = _denom;
Numer = _numer;
}
int Fraction::getNumberOfFraction()
{
return countFraction;
}
File main.cpp
int Fraction::countFraction = 1;
int _tmain(int argc, _TCHAR* argv[])
{
cout << Fraction::getNumberOfFraction();
return 0;
}
Và bị lỗi Error C2352: illegal call of non-static member function.
Nhưng nếu gọi hàm thông qua tạo một đối tượng thì nó lại chạy bình thường. Ai giải thích hộ em với. Hàm tĩnh - static gọi bằng tên lớp mà? Sao lỗi là gọi hàm non - static member function?
Code C# lâu quá về C++ làm rối hết