![](/img/trans.png)
[英]expected constructor, destructor, or type conversion before ‘*’ token
[英]expected constructor, destructor, or type conversion before '<<' token
在使用 g++ 编译后续程序时。
#include<iostream>
using namespace std;
cout<<"Before Main"<<endl;
int main()
{
cout<<"Within Main"<<endl;
}
错误:'<<' 标记之前的预期构造函数、析构函数或类型转换。 所以我可以知道如何解决这个问题。 出现错误的原因是什么。
您不能在 function 之外执行语句。
您不能在命名空间 scope 中放置非声明语句。
但是,表达式语句可以转换为声明,例如
bool const bah = (cout<<"Before Main"<<endl);
这通常不是一个好主意,但也许值得了解?
干杯&hth.,
如果不将其放入 function 主体中,则无法执行此类语句。 如果你在main()
之前需要一些东西,那么将它封装在一个全局结构中并定义一个 object。
struct Print {
Print() { cout<<"Before Main"<<endl; }
~Print() { cout<<"After Main"<<endl; }
} print; // <--- declare/define object
int main()
{
cout<<"Within Main"<<endl;
}
这是非法的,将要执行的所有内容都必须在主 function 内。 当然你可以写第二个 function,然后把cout<<"Before Main"<<endl;
, 但 main 将首先执行。 你可以写:
#include<iostream>
using namespace std;
void f() {
cout<<"Before Main"<<endl;
}
int main()
{
f();
cout<<"Within Main"<<endl;
}
但主要是第一个 function 将被执行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.