如下程序编译有关问题,是什么原因造成的异常,该如何解决
如下程序编译问题,是什么原因造成的错误,该怎么解决?
#include "stdafx.h"
#include <iostream>
using namespace std;
#ifdef __cplusplus
cout << "c++" << endl; //这里一定要有输出语句的。
#else
cout << "c" << endl; //这里一定要有输出语句的。
#endif
int main()
{
return 0;
}
编译提示如下:
--------------------Configuration: 2 - Win32 Debug--------------------
Compiling...
2.cpp
F:\VC工程\2\2.cpp(11) : error C2143: syntax error : missing ';' before '<<'
F:\VC工程\2\2.cpp(11) : error C2501: 'cout' : missing storage-class or type specifiers
F:\VC工程\2\2.cpp(11) : error C2143: syntax error : missing ';' before '<<'
Error executing cl.exe.
2.exe - 3 error(s), 0 warning(s)
------解决方案--------------------
你把cout放主函数外什么意思?你认为有全局的cout和cin么?
------解决方案--------------------
#ifdef __cplusplus
cout << "c++" << endl; //这里一定要有输出语句的。
#else
cout << "c" << endl; //这里一定要有输出语句的。
#endif
这些不过是条件编译而已,真正有效的是cout << "c++" << endl;或cout << "c" << endl;
这是普通的语句而已,当然要写在函数体里面了
------解决方案--------------------
语句是不能放到函数外面的,是不会执行的。
------解决方案--------------------
------解决方案--------------------
------解决方案--------------------
二楼正解
对啊 ,你这个是明显错误的,如果你在预编译中式做函数的定义或者是头文件包含这些都没问题,但是你做了函数调用cout << "c++" << endl; //这里一定要有输出语句的。 由于你的是c++的环境,所以#ifdef __cplusplus是true ,然后就执行cout << "c++" << endl;这句了,这个肯定编译通不过啊。
------解决方案--------------------
把语句放到函数里面来吧
#include "stdafx.h"
#include <iostream>
using namespace std;
#ifdef __cplusplus
cout << "c++" << endl; //这里一定要有输出语句的。
#else
cout << "c" << endl; //这里一定要有输出语句的。
#endif
int main()
{
return 0;
}
编译提示如下:
--------------------Configuration: 2 - Win32 Debug--------------------
Compiling...
2.cpp
F:\VC工程\2\2.cpp(11) : error C2143: syntax error : missing ';' before '<<'
F:\VC工程\2\2.cpp(11) : error C2501: 'cout' : missing storage-class or type specifiers
F:\VC工程\2\2.cpp(11) : error C2143: syntax error : missing ';' before '<<'
Error executing cl.exe.
2.exe - 3 error(s), 0 warning(s)
------解决方案--------------------
你把cout放主函数外什么意思?你认为有全局的cout和cin么?
------解决方案--------------------
#ifdef __cplusplus
cout << "c++" << endl; //这里一定要有输出语句的。
#else
cout << "c" << endl; //这里一定要有输出语句的。
#endif
这些不过是条件编译而已,真正有效的是cout << "c++" << endl;或cout << "c" << endl;
这是普通的语句而已,当然要写在函数体里面了
------解决方案--------------------
语句是不能放到函数外面的,是不会执行的。
------解决方案--------------------
------解决方案--------------------
------解决方案--------------------
二楼正解
对啊 ,你这个是明显错误的,如果你在预编译中式做函数的定义或者是头文件包含这些都没问题,但是你做了函数调用cout << "c++" << endl; //这里一定要有输出语句的。 由于你的是c++的环境,所以#ifdef __cplusplus是true ,然后就执行cout << "c++" << endl;这句了,这个肯定编译通不过啊。
------解决方案--------------------
把语句放到函数里面来吧