#include <iostream>using namespace std;class A{public: A(int x,int y,int z):a(x),b(y),c(z) {} friend ostream & operator << (ostream & ,const A &); int a,b,c;};ostream & operator << (ostream &os,const A &x){ os<<x.a<<x.b<<x.c; return os;}void main(){ A a(1,2,3); cout<<a<<endl;}
不知道你用的什么编译器我用 devcpp4.9.92编译你的代码没有问题(但要把void main改成int main,void main()确实是非标准的)如果你用的是VC6.0的话,应该是编译器的问题.换个对标准支持好的编译器吧。(也不能怪VC6.0它出的时候C++98标准还没出呢)对于int main 和 void main的讨论可以看看这篇文章为什么void main()是错误的 http://www.cfannet.com/bbs/dispbbs.asp?boardID=9&ID=1&page=2
提问者对答案的评价:
======================能不能把错误信息打出来======================
将代码中void main()改为int main()很多人甚至市面上的一些书籍,都使用了void main( ) ,其实这是错误的。C/C++ 中从来没有定义过void main( ) 。C++ 之父 Bjarne Stroustrup 在他的主页上的 FAQ 中明确地写着 The definition void main( ) { /* ... */ } is not and never has been C++, nor has it even been C.( void main( ) 从来就不存在于 C++ 或者 C )。 |