449,024 Members | 903 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,024 IT Pros & Developers. It's quick & easy.

# template function member with const classifier

 P: n/a Hi, I'm trying to create an template class that represent points in all possible dimensions, what I've made until now is #ifndef _POINT_HPP_ #define _POINT_HPP_ #include template class Point{ private: std::vector coordinates; public: explicit Point( int n ); Point( const Point& other ); Point( const std::vector& coord ); ~Point(); std::vector Get_Coordinates() const; void Set_Coordinates( const std::vector& coord ); void Translate( const std::vector& coord ); void Scale( const T& factor ); //unsigned int Distance( const Point& other ); //unsigned int Distance_Origin() void WriteLn(); private: static const int Expt_Different_Sizes; }; template Point::Point( int n ): coordinates( n ) { } template Point::Point( const Point& other ): coordinates( other.coordinates ) { } template Point::Point( const std::vector& coord ): coordinates( coord ) { } template Point::~Point() { } template void Point::Set_Coordinates( const std::vector& coord ) { if( coordinates.size() != coord.size() ) throw Point::Expt_Different_Sizes; else { for( int i=0; i std::vector Get_Coordinates() const { return coordinates; } template void Point::Translate( const std::vector& coord ) { if( coordinates.size() != coord.size() ) throw Point::Expt_Different_Sizes; // Add the two coordinates for( int i = 0; i < coordinates.size(); ++i ) coordinates[i] = coordinates[i] + coord[i]; } template void Point::Scale( const T& factor ) { if( factor == 0 ) throw Point::Expt_Divide_By_Zero; else { for( int i = 0; i< coordinates.size(); i++ ) coordinates[i] = coordinates[i]/factor; } } template void Point::WriteLn() { typename std::vector::iterator pos; for( pos = coordinates.begin(); pos < coordinates.end(); ++pos) std::cout << *pos << " "; std::cout << std::endl; } // Specify the exception code template const int Point::Expt_Different_Sizes = 1; template const int Point::Expt_Divide_By_Zero = 2; #endif nt::Expt_Different_Sizes; // Add the two coordinates for( int i = 0; i < coordinates.size(); ++i ) coordinates[i] = coordinates[i] + coord[i]; } template void Point::Scale( const T& factor ) { if( factor == 0 ) throw Point::Expt_Divide_By_Zero; else { for( int i = 0; i< coordinates.size(); i++ ) coordinates[i] = coordinates[i]/factor; } } template void Point::WriteLn() { typename std::vector::iterator pos; for( pos = coordinates.begin(); pos < coordinates.end(); ++pos) std::cout << *pos << " "; std::cout << std::endl; } // Specify the exception code template const int Point::Expt_Different_Sizes = 1; template const int Point::Expt_Divide_By_Zero = 2; #endif But I'm getting the errors In file included from src/GUI.cpp:12: include/point.hpp:84: error: non-member function `std::vector > Get_Coordinates()' cannot have `const' method qualifier include/point.hpp:127: error: `const int Point::Expt_Divide_By_Zero' is not a static member of `class Point' include/point.hpp:127: error: template definition of non-template `const int Point::Expt_Divide_By_Zero' make: *** [GUI.o] Error 1 Can any one give me an inch about want I'm doing wrong? If you could send me some links I would appreciate. Thanks in advance Pedro Sousa -- Posted via a free Usenet account from http://www.teranews.com Jun 14 '06 #1
5 Replies

 P: n/a Pedro Sousa wrote: Hi, I'm trying to create an template class that represent points in all possible dimensions, what I've made until now is #ifndef _POINT_HPP_ #define _POINT_HPP_ All names starting with an underscore and capital letter are reserved for use by the implementation. Change this. #include template class Point{ private: std::vector coordinates; public: explicit Point( int n ); Point( const Point& other ); Point( const std::vector& coord ); ~Point(); std::vector Get_Coordinates() const; void Set_Coordinates( const std::vector& coord ); void Translate( const std::vector& coord ); void Scale( const T& factor ); //unsigned int Distance( const Point& other ); //unsigned int Distance_Origin() void WriteLn(); private: static const int Expt_Different_Sizes; }; template Point::Point( int n ): coordinates( n ) { } template Point::Point( const Point& other ): coordinates( other.coordinates ) { } template Point::Point( const std::vector& coord ): coordinates( coord ) { } template Point::~Point() { } template void Point::Set_Coordinates( const std::vector& coord ) { if( coordinates.size() != coord.size() ) throw Point::Expt_Different_Sizes; else { for( int i=0; i std::vector Get_Coordinates() const { return coordinates; } template void Point::Translate( const std::vector& coord ) { if( coordinates.size() != coord.size() ) throw Point::Expt_Different_Sizes; // Add the two coordinates for( int i = 0; i < coordinates.size(); ++i ) coordinates[i] = coordinates[i] + coord[i]; } template void Point::Scale( const T& factor ) { if( factor == 0 ) throw Point::Expt_Divide_By_Zero; else { for( int i = 0; i< coordinates.size(); i++ ) coordinates[i] = coordinates[i]/factor; } } template void Point::WriteLn() { typename std::vector::iterator pos; for( pos = coordinates.begin(); pos < coordinates.end(); ++pos) std::cout << *pos << " "; std::cout << std::endl; } // Specify the exception code template const int Point::Expt_Different_Sizes = 1; template const int Point::Expt_Divide_By_Zero = 2; #endif [snip copy and paste error] But I'm getting the errors In file included from src/GUI.cpp:12: include/point.hpp:84: error: non-member function `std::vector > Get_Coordinates()' cannot have `const' method qualifier That's because Get_Coordinates is not a member of your class, but it should be since it accesses your class members. See also this FAQ: http://www.parashift.com/c++-faq-lit...html#faq-18.10 include/point.hpp:127: error: `const int Point::Expt_Divide_By_Zero' is not a static member of `class Point' This one is quite straight forward: the constant is not a member of your class at all. include/point.hpp:127: error: template definition of non-template `const int Point::Expt_Divide_By_Zero' This is because of the previous error. make: *** [GUI.o] Error 1 Can any one give me an inch about want I'm doing wrong? If you could send me some links I would appreciate. If we give you an inch, you might take a mile. So, no. But I have given you some hints. :-) Cheers! --M Jun 14 '06 #2