【问题标题】:RDTSC on VisualStudio 2010 Express - C++ does not support default-intVisualStudio 2010 Express 上的 RDTSC - C++ 不支持默认整数
【发布时间】:2013-08-24 11:12:08
【问题描述】:

我尝试在 VisualStudio 2010 上测试 rdtsc。这是我的代码:

#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

【问题讨论】:

    标签: c++ visual-studio-2010 visual-studio rdtsc


    【解决方案1】:

    你必须#include &lt;stdint.h&gt;。或者(更好)#include &lt;cstdint&gt;

    Visual Studio 开始在 2010 版本中提供这些标头。

    【讨论】:

      【解决方案2】:

      要使其正常工作,您必须包含 cstdint

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

      cstdint 是 C 样式标头 stdint.h 的 C++ 样式版本。那么在你的情况下最好使用第一个,即使两者都在 C++ 中工作。

      据说here自 2010 版本以来这些标头随 Visual Studio 一起提供。

      【讨论】:

      • 这是一个 C++ 源文件,这里正在构建。请参阅它提到 main.cpp 的初始帖子。
      • 然后?这就是我首先谈论&lt;cstdint&gt; 的原因。我不明白你的意思。
      • 那你为什么还要提到C呢?
      • 但是为什么它是一个 C++ 问题呢? :)
      【解决方案3】:

      您显然没有在顶部包含 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;
      }
      

      【讨论】:

        猜你喜欢
        • 2015-06-13
        • 2011-11-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多