意外的非限定 ID 错误

unexpected unqualified id error

本文关键字:ID 错误 意外      更新时间:2023-10-16
expected unqualified-id before string constant in member function set_linux().

我在编译代码期间收到此错误。我在这里添加了类和主函数。

expected before '}' before end of line

法典:

#include "Timer.h"
Timer::Timer() {
    now = 0;
    is_init = 0;
    #if defined (_linux)
        set_linux();
    #elif defined (_APPLE)
        set_apple();
    #elif defined (_WIN32)
        set_win32();
    #endif // defined
}
void Timer::set_apple() {
    #define HAVE_MACH_TIMER
    //#include <mach/mach_time.h>
}
void Timer::set_linux() {
    #define HAVE_POSIX_TIMER
    #include <time.h>
    #ifdef CLOCK_MONOTONIC
        #define CLOCKID CLOCK_MONOTONIC
    #else
        #define CLOCKID CLOCK_REALTIME
    #endif // CLOCK_MONOTONIC
}
void Timer::set_win32() {
    #define WIN32_LEAN_AND_MEAN
    #include <windows.h>
}
void Timer::apple() {
    static mach_timebase_info_data_t info;
    if (0 == is_init) {
        mach_timebase_info(&info);
        is_init = 1;
    }
    now = mach_absolute_time();
    now *= info.numer;
    now /= info.denom;
}
void Timer::linux() {
    static struct timespec linux_rate;
    if (0 == is_init) {
        clock_getres(CLOCKID, &linux_rate);
        is_init = 1;
    }
    struct timespec spec;
    clock_gettime(CLOCKID, &spec);
    now = spec.tv_sec * 1.0e9 + spec.tv_nsec;
}
void Timer::win32() {
    static LARGE_INTEGER win_frequency;
    if (0 == is_init) {
        QueryPerformanceFrequency(&win_frequency);
        is_init = 1;
    }
    LARGE_INTEGER now;
    QueryPerformanceCounter(&now);
    this->now = (uint64_t) ((1e9 * now.QuadPart) / win_frequency.QuadPart);
}
static uint64_t Timer::ns() {
    #if defined (_APPLE_)
        apple();
        return now;
    #elif defined (_linux)
        linux();
        return now;
    #elif defined(_WIN32)
        win32();
        return now;
    #endif // defined
}
using namespace std;
int main() {
 return 0;
}

你不应该在函数或命名空间范围内 #include 东西,因为这会使它更难看到(在这种情况下,你是在局部作用域下定义它的内容,而不是它们所在的全局作用域(。 此外,您正在执行的操作会导致每个平台上的编译问题。 您可能想要的是有条件地 #include 并根据顶部的平台 #define 您的价值观。

#include "Timer.h"
#if defined (_linux)
# define HAVE_POSIX_TIMER
# include <time.h>
# ifdef CLOCK_MONOTONIC
#  define CLOCKID CLOCK_MONOTONIC
# else
#  define CLOCKID CLOCK_REALTIME
# endif // CLOCK_MONOTONIC
#elif defined (_APPLE)
# define HAVE_MACH_TIMER
//# include <mach/mach_time.h>
#elif defined (_WIN32)
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
#endif
Timer::Timer() {
    now = 0;
    is_init = 0;
    // This portion may no longer be applicable.
#if defined (_linux)
    set_linux();
#elif defined (_APPLE)
    set_apple();
#elif defined (_WIN32)
    set_win32();
#endif // defined
}
void Timer::set_apple() {
}
void Timer::set_linux() {
}
void Timer::set_win32() {
}
void Timer::apple() {
    static mach_timebase_info_data_t info;
    if (0 == is_init) {
        mach_timebase_info(&info);
        is_init = 1;
    }
    now = mach_absolute_time();
    now *= info.numer;
    now /= info.denom;
}
void Timer::linux() {
    static struct timespec linux_rate;
    if (0 == is_init) {
        clock_getres(CLOCKID, &linux_rate);
        is_init = 1;
    }
    struct timespec spec;
    clock_gettime(CLOCKID, &spec);
    now = spec.tv_sec * 1.0e9 + spec.tv_nsec;
}
void Timer::win32() {
    static LARGE_INTEGER win_frequency;
    if (0 == is_init) {
        QueryPerformanceFrequency(&win_frequency);
        is_init = 1;
    }
    LARGE_INTEGER now;
    QueryPerformanceCounter(&now);
    this->now = (uint64_t) ((1e9 * now.QuadPart) / win_frequency.QuadPart);
}
static uint64_t Timer::ns() {
#if defined (_APPLE_)
        apple();
        return now;
#elif defined (_linux)
        linux();
        return now;
#elif defined(_WIN32)
        win32();
        return now;
#endif // defined
}
using namespace std;
int main() {
 return 0;
}