2013-08-24 67 views
2

我想在2010年的VisualStudio繼承人測試rdtsc我的代碼:RDTSC上的VisualStudio 2010速成 - C++不支持默認int

#include <iostream> 
#include <windows.h> 
#include <intrin.h> 
using namespace std; 

uint64_t rdtsc() 
{ 
    return __rdtsc(); 
} 

int main() 
{ 
    cout << rdtsc() << "\n"; 
    cin.get(); 
    return 0; 
} 

但我得到的錯誤:

------ Build started: Project: test_rdtsc, Configuration: Debug Win32 ------ 
    main.cpp 
c:\documents and settings\student\desktop\test_rdtsc\test_rdtsc\main.cpp(12): error C2146: syntax error : missing ';' before identifier 'rdtsc' 
c:\documents and settings\student\desktop\test_rdtsc\test_rdtsc\main.cpp(12): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
c:\documents and settings\student\desktop\test_rdtsc\test_rdtsc\main.cpp(13): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
c:\documents and settings\student\desktop\test_rdtsc\test_rdtsc\main.cpp(14): warning C4244: 'return' : conversion from 'DWORD64' to 'int', possible loss of data 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 

什麼都要我做?我不想將uint64_t更改爲DWORD64。爲什麼VisualStudio不理解uint64_t

回答

3

您必須#include <stdint.h>。或者(更好)#include <cstdint>

Visual Studio開始將​​這些標頭與2010版本一起發貨。

1

顯然,您尚未在頂部包含stdint.h/cstdint。這將工作:

#include <iostream> 
#include <windows.h> 
#include <intrin.h> 
#include <stdint.h> 
using namespace std; 

uint64_t rdtsc() 
{ 
    return __rdtsc(); 
} 

int main() 
{ 
    cout << rdtsc() << "\n"; 
    cin.get(); 
    return 0; 
} 
2

要有這方面的工作,你必須包含cstdint

#include <cstdint> // Or <stdint.h> 

cstdint是C++ - C風格的頭stdint.h風格的版本。那麼在你的情況下最好使用第一個,即使它們都用C++工作。

據說here自2010版以來,這些頭文件隨Visual Studio一起提供。

+0

這是一個C++源文件,這裏正在構建什麼。看到它提到main.cpp的初始文章。 – lpapp

+0

而且?這就是我首先談論''的原因。我不明白你的觀點。 –

+0

爲什麼你甚至提到C呢? – lpapp