在VS 2010下编译C代码
compiling C code under VS 2010
我试图在MS VS 2010下编译旧的C代码。错误发生在结构体声明和调用。
声明(编辑):
typedef struct tStr
{
int nInt;
int ***anPoint;
};
用法:
struct tStr tStuff;
tStuff.nInt = 0;
函数声明(编辑:现在可能有效):
int doStuff(struct tStr *sStuff, int nStuff);
编译器报错未识别的标识符,缺少(或;或者{等等。它们都被归类为语法错误。我检查的语法应该没问题。所以我在我的结束…
我重新做了结构声明。但还是同样的错误:
error C2143: syntax error : missing ';' before 'type'
好,回到原来的声明。但是,如果我尝试像使用时那样访问结构变量,我会得到一个错误:
error C2065: 'tStuff' : undeclared identifier
所以我的代码是:
typedef struct tMatrix
{
int nRows;
int nCols;
int nVec;
int ***anMatrix;
};
int allocateMatrix(struct tMatrix *sMatrix, int nType);
struct tMatrix sMatrix1;
sMatrix1.nRows = 0;
错误:
error C2143: syntax error : missing ';' before 'type'
error C2065: 'sMatrix1' : undeclared identifier
任何想法?
谢谢你的回答,我想我还是重写一遍吧。我还遇到了更多关于类型转换的错误。为了避免头痛,我就重新开始。再次感谢。简短的回答是您缺少typedef
的一个参数(继续获得完整的解释)。
typedef
的语法为:
typepedef 类型定义标识符;
在您的示例中,类型定义是struct tStr
,并且缺少标识符。
如果你想从你的结构中创建一个新的类型定义,你可以这样声明它:
typedef struct tStr_ {
int nInt;
int ***anPoint;
} tStr;
现在您可以在代码中引用新类型tStr
:
tStr tStuff;
tStuff.nInt = 0;
在本例中,tStr_
是结构体的名称,tStr
是新类型的名称。您仍然可以通过其名称引用该结构体:
struct tStr_ tStuff;
tStuff.nInt = 0;
编辑:也许我们需要一个更完整的例子来说明你想要完成的任务。以下代码示例编译时没有出现错误,并产生预期的结果:
#include <stdio.h>
#include <stdlib.h>
typedef struct tMatrix_ {
int nRows;
int nCols;
int nVec;
int ***anMatrix;
} tMatrix;
int allocateMatrix(tMatrix *sMatrix, int nType)
{
sMatrix->nRows = 10;
return 0;
}
int main(void)
{
tMatrix sMatrix1;
allocateMatrix(&sMatrix1, 0);
printf("%dn", sMatrix1.nRows);
return EXIT_SUCCESS;
}
除非你在C中定义了一个结构体,否则用法应该是:
struct TStr tStuff;
您是否在项目文件中使用/Tc命令行参数?http://msdn.microsoft.com/en-us/library/032xwy55.aspx
相关文章:
- 如何修复sfml c++代码编译错误
- 尝试用java代码编译和运行c++代码
- 此代码编译良好,但文件未创建?请指出错误
- 为什么我的 BaseClass:Method 代码编译(带有单冒号)?
- 代码编译没有任何输出,入门程序
- 通过Python Distutils(用于Python C扩展)使用可重定位的设备代码编译CUDA代码
- 代码编译但不起作用!cmd窗口只是理想和理想,但什么也没发生
- 无法使用 g++ 使用C++代码编译 C 库
- 如何在Ubuntu中使用Visual Studio代码编译C++代码
- 推力+提升代码编译错误
- C++代码编译,但在 Zorin OS 上运行时显示错误
- 如何在使用模板时将 CPP 代码编译到库文件中
- 是否可以将Visual Studio 2017将C 代码编译到EXE以外的文件类型中
- 使用 Visual Studio for C++ 代码编译错误
- 使用 Android Studio 使用本机代码编译 apk 时,如何在链接处删除 libgnustl_static.
- 使用 Emscripten 将 OpenCV 代码编译C++ Javascript
- 将C 代码编译到独立应用程序.App
- 为什么此代码编译 (C++11) 而没有类型不匹配错误
- 重用编译器前端的结果,以加快多个配置/平台的C++代码编译
- 如何将C++11代码编译为网络汇编