意外的不合格ID错误

时间:2016-01-12 18:21:03

标签: c++

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;
}

1 个答案:

答案 0 :(得分:1)

你不应该在函数或命名空间范围内#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;
}