【问题标题】:Visual studio 14.0 giving compile errors with cstdint and stdint.hVisual Studio 14.0 使用 cstdint 和 stdint.h 给出编译错误
【发布时间】:2018-03-14 02:03:40
【问题描述】:

我正在尝试使用学校的 allegro 库构建一个 c++ 程序,但我似乎无法通过处理 cstdint 和 stdint.h 的大量编译错误列表

从我的文件中删除所有代码会给我一个头文件

#pragma once
#include <allegro.h>
#include <cstdlib>
#include <cstdio>
#include <sstream>
#include <fstream>

而我的主文件只包含

#include "Trivia.h"

这是我正在尝试处理的错误

1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\stdint.h(17): warning C4114: same type qualifier used more than once
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\stdint.h(17): error C2632: 'char' followed by 'char' is illegal
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\stdint.h(17): warning C4091: 'typedef ': ignored on left of 'signed char' when no variable is declared
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\stdint.h(18): error C2632: 'short' followed by 'short' is illegal
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\stdint.h(18): warning C4091: 'typedef ': ignored on left of 'short' when no variable is declared
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\stdint.h(19): error C2632: 'int' followed by 'int' is illegal
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\stdint.h(19): warning C4091: 'typedef ': ignored on left of 'int' when no variable is declared
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\stdint.h(20): error C2632: '__int64' followed by '__int64' is illegal
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\stdint.h(20): warning C4091: 'typedef ': ignored on left of '__int64' when no variable is declared
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\stdint.h(21): warning C4114: same type qualifier used more than once
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\stdint.h(21): error C2632: 'char' followed by 'char' is illegal
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\stdint.h(21): warning C4091: 'typedef ': ignored on left of 'unsigned char' when no variable is declared
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\stdint.h(22): warning C4114: same type qualifier used more than once
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\stdint.h(22): error C2632: 'short' followed by 'short' is illegal
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\stdint.h(22): warning C4091: 'typedef ': ignored on left of 'unsigned short' when no variable is declared
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\stdint.h(23): warning C4114: same type qualifier used more than once
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\stdint.h(23): error C2632: 'int' followed by 'int' is illegal
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\stdint.h(23): warning C4091: 'typedef ': ignored on left of 'unsigned int' when no variable is declared
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\stdint.h(24): warning C4114: same type qualifier used more than once
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\stdint.h(24): error C2632: '__int64' followed by '__int64' is illegal
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\stdint.h(24): warning C4091: 'typedef ': ignored on left of 'unsigned __int64' when no variable is declared
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\cstdint(18): error C2589: 'signed': illegal token on right side of '::'
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\cstdint(18): error C2059: syntax error: '::'
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\cstdint(19): error C2589: 'signed': illegal token on right side of '::'
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\cstdint(19): error C2059: syntax error: '::'
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\cstdint(20): error C2589: 'unsigned': illegal token on right side of '::'
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\cstdint(20): error C2059: syntax error: '::'
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\cstdint(21): error C2589: 'unsigned': illegal token on right side of '::'
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\cstdint(21): error C2059: syntax error: '::'
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\cstdint(33): error C2589: 'signed': illegal token on right side of '::'
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\cstdint(33): error C2059: syntax error: '::'
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\cstdint(34): error C2589: 'unsigned': illegal token on right side of '::'
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\cstdint(34): error C2059: syntax error: '::'
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\cstdint(37): error C2589: 'signed': illegal token on right side of '::'
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\cstdint(37): error C2059: syntax error: '::'
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\cstdint(38): error C2589: 'signed': illegal token on right side of '::'
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\cstdint(38): error C2059: syntax error: '::'
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\cstdint(39): error C2589: 'unsigned': illegal token on right side of '::'
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\cstdint(39): error C2059: syntax error: '::'
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\cstdint(40): error C2589: 'unsigned': illegal token on right side of '::'
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\cstdint(40): error C2059: syntax error: '::'
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\cstdint(52): error C2589: 'signed': illegal token on right side of '::'
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\cstdint(52): error C2059: syntax error: '::'
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\cstdint(53): error C2589: 'unsigned': illegal token on right side of '::'
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\cstdint(53): error C2059: syntax error: '::'

如果我删除所有 stdio.h、sstream 和 fstream,我可以让错误消失,但我的任务需要这些。

【问题讨论】:

  • 与您的问题无关,但您的使用或#pragma once 表明您显示的代码来自头文件,在这种情况下using namespace std 是错误的,甚至可能是危险的(不是说它在一个源文件)。我建议您花点时间阅读Why is “using namespace std” considered bad practice?,并真正花时间阅读所有个答案。
  • 至于您的错误,您向我们展示的代码之前在做什么?你在哪里包含你的头文件?你在那里做什么?您真的需要创建一个Minimal, Complete, and Verifiable Example 并向我们展示!
  • 删除using namespace std;
  • 再说一次,之前 #include "Trivia.h"你在做什么? Minimal, Complete, and Verifiable Example 在哪里?
  • 创建一个空的 C++ 项目,添加 Trivia.h 和 Trivia.cpp 并准确输入上述内容会出现这些错误。

标签: c++ visual-c++ visual-studio-2015


【解决方案1】:
#include <stdlib.h>
#include <stdio.h>

它们是 C 标头,您需要 C++ 标头:

#include <cstdlib>
#include <cstdio>

编辑:

首先你有一堆来自stdint.hC 标头!)的错误,然后来自cstrint。只是猜测,但如果allegroC 库(不是C++),您还需要更改

#include <allegro.h>

extern "C"
{
    #include <allegro.h>
}

【讨论】:

  • 试过了,好像没什么变化。仍然遇到同样的错误。
  • Allegro 旨在与 C++ 一起使用,这样做会给我“C 链接函数无法返回 C++ 类‘修复’错误。
【解决方案2】:

显然我可以通过在 sstream 和 fstream 之后包含 allegro.h 来解决这个问题。我不知道为什么这可以解决问题(而且我首先看到了包括 allegro.h 在内的其他代码),但至少它可以工作。

【讨论】:

猜你喜欢
  • 2012-11-18
  • 2015-04-12
  • 1970-01-01
  • 2015-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-06
相关资源
最近更新 更多