| |||
| #include <iostream> #include <cstdlib> #include <ctime> #include <iomanip> using namespace std; class Point { friend ostream &operator<<( ostream &, const Point & ); public: Point( double=0, double=0, double=0 ); void setPoint( double, double, double ); double getX() const { return x; } double getY() const { return y; } double getZ() const { return z; } protected: double x,y,z; }; Point::Point( double a, double b, double c ) { setPoint( a, b, c ); } void Point: etPoint( double a, double b, double c ){ x=a; y=b; z=c; } ostream &operator<<( ostream &output, const Point &p ) { output << setiosflags( ios::fixed | ios: howpoint )<< setprecision(2) << '[' << p.x << "," << p.y << "," << p.z << "]"; return output; } int main() { int SIZE=10; Point p[10]; srand( time(NULL) ); for ( int i=0; i<SIZE; i++ ) p[i].setPoint( (rand()%100 ) / float(100), rand()%100/float(100), 0.03 ); for ( int i=0; i<SIZE; i++ ) { cout << p[i] << endl; } system( "pause" ); return 0; } |
| |||
| 我把程序编译了,但是出现了5个错误呢? U:\C++\renyi\ren.cpp(36) : error C2248: 'x' : cannot access protected member declared in class 'Point' U:\C++\renyi\ren.cpp(1 U:\C++\renyi\ren.cpp(36) : error C2248: 'y' : cannot access protected member declared in class 'Point' U:\C++\renyi\ren.cpp(1 U:\C++\renyi\ren.cpp(36) : error C2248: 'z' : cannot access protected member declared in class 'Point' U:\C++\renyi\ren.cpp(1 U:\C++\renyi\ren.cpp(52) : error C2374: 'i' : redefinition; multiple initialization U:\C++\renyi\ren.cpp(4 U:\C++\renyi\ren.cpp(53) : error C2593: 'operator <<' is ambiguous Error executing cl.exe. renyi.exe - 5 error , 0 warning![]() 怎麽办呢? |
| |||
| 我把程序编译了,但是出现了5个错误呢? U:\C++\renyi\ren.cpp(36) : error C2248: 'x' : cannot access protected member declared in class 'Point' U:\C++\renyi\ren.cpp(1 U:\C++\renyi\ren.cpp(36) : error C2248: 'y' : cannot access protected member declared in class 'Point' U:\C++\renyi\ren.cpp(1 U:\C++\renyi\ren.cpp(36) : error C2248: 'z' : cannot access protected member declared in class 'Point' U:\C++\renyi\ren.cpp(1 U:\C++\renyi\ren.cpp(52) : error C2374: 'i' : redefinition; multiple initialization U:\C++\renyi\ren.cpp(4 U:\C++\renyi\ren.cpp(53) : error C2593: 'operator <<' is ambiguous Error executing cl.exe. renyi.exe - 5 error , 0 warning![]() 怎麽办呢? |
| |||
| 我用的是 dev c++ 编译通过的 用不同编译器 编译器 是不同的 你用的是什么编译器?? 根据你的错误信息 改了一下 试试吧 #include <iostream> #include <cstdlib> #include <ctime> #include <iomanip> using namespace std; class Point { public: Point( double=0, double=0, double=0 ); void setPoint( double, double, double ); double getX() const { return x; } double getY() const { return y; } double getZ() const { return z; } private: double x,y,z; }; Point::Point( double a, double b, double c ) { setPoint( a, b, c ); } void Point: etPoint( double a, double b, double c ) { x=a; y=b; z=c; } int main() { int SIZE=10; Point p[10]; srand( time(NULL) ); for ( int i=0; i<SIZE; i++ ) { p[i].setPoint( (rand()%100 ) / float(100), rand()%100/float(100), 0.03 ); } for ( int j=0; j<SIZE; j++ ) { cout << setiosflags( ios::fixed | ios: howpoint ) << setprecision(2) << '[' << p[j].getX() << "," << p[j].getY() << "," << p[j].getZ() << "]"; cout << endl; } system( "pause" ); return 0; } |
| |||
| srand( time(NULL) ); for ( int i=0; i<SIZE; i++ ) { p[i].setPoint( (rand()%100 ) / float(100), rand()%100/float(100), 0.03 ); } 其实关键就在这里。。其他的代码不是很重要。 我用的方法还是c的, 而不是c++推荐的。。 我写的只是参考 你还是要自己实践一下的,只是为了应付。骗的只是自己而已。 我也是新手 看了一通书 有点消化不了。现在是c++ 连 stl一起啃 最近我会长期泡在这里。 希望大家一起切磋 |