By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,855 Members | 1,988 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,855 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 <vector>

template <typename T>
class Point{
private:
std::vector<T> coordinates;

public:
explicit Point( int n );

Point( const Point<T>& other );

Point( const std::vector<T>& coord );

~Point();

std::vector<T> Get_Coordinates() const;

void Set_Coordinates( const std::vector<T>& coord );

void Translate( const std::vector<T>& coord );

void Scale( const T& factor );

//unsigned int Distance( const Point<T>& other );

//unsigned int Distance_Origin()
void WriteLn();

private:
static const int Expt_Different_Sizes;

};

template <typename T>
Point<T>::Point( int n ):
coordinates( n )
{
}

template <typename T>
Point<T>::Point( const Point<T>& other ):
coordinates( other.coordinates )
{
}

template <typename T>
Point<T>::Point( const std::vector<T>& coord ):
coordinates( coord )
{
}

template <typename T>
Point<T>::~Point()
{
}

template <typename T>
void Point<T>::Set_Coordinates( const std::vector<T>& coord )
{
if( coordinates.size() != coord.size() )
throw Point<T>::Expt_Different_Sizes;
else
{
for( int i=0; i<coordinates.size(); i++ )
coordinates[i] = coord[i];
}
}

template <typename T>
std::vector<T> Get_Coordinates() const
{
return coordinates;
}

template <typename T>
void Point<T>::Translate( const std::vector<T>& coord )
{
if( coordinates.size() != coord.size() )
throw Point<T>::Expt_Different_Sizes;

// Add the two coordinates
for( int i = 0; i < coordinates.size(); ++i )
coordinates[i] = coordinates[i] + coord[i];
}

template <typename T>
void Point<T>::Scale( const T& factor )
{
if( factor == 0 )
throw Point<T>::Expt_Divide_By_Zero;
else
{
for( int i = 0; i< coordinates.size(); i++ )
coordinates[i] = coordinates[i]/factor;
}
}

template <typename T>
void Point<T>::WriteLn()
{
typename std::vector<T>::iterator pos;

for( pos = coordinates.begin(); pos < coordinates.end(); ++pos)
std::cout << *pos << " ";

std::cout << std::endl;
}

// Specify the exception code
template <typename T>
const int Point<T>::Expt_Different_Sizes = 1;

template <typename T>
const int Point<T>::Expt_Divide_By_Zero = 2;

#endif
nt<T>::Expt_Different_Sizes;

// Add the two coordinates
for( int i = 0; i < coordinates.size(); ++i )
coordinates[i] = coordinates[i] + coord[i];
}

template <typename T>
void Point<T>::Scale( const T& factor )
{
if( factor == 0 )
throw Point<T>::Expt_Divide_By_Zero;
else
{
for( int i = 0; i< coordinates.size(); i++ )
coordinates[i] = coordinates[i]/factor;
}
}

template <typename T>
void Point<T>::WriteLn()
{
typename std::vector<T>::iterator pos;

for( pos = coordinates.begin(); pos < coordinates.end(); ++pos)
std::cout << *pos << " ";

std::cout << std::endl;
}

// Specify the exception code
template <typename T>
const int Point<T>::Expt_Different_Sizes = 1;

template <typename T>
const int Point<T>::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<T,
std::allocator<_CharT> > Get_Coordinates()' cannot have `const' method
qualifier
include/point.hpp:127: error: `const int Point<T>::Expt_Divide_By_Zero'
is not
a static member of `class Point<T>'
include/point.hpp:127: error: template definition of non-template `const
int
Point<T>::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
Share this Question
Share on Google+
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 <vector>

template <typename T>
class Point{
private:
std::vector<T> coordinates;

public:
explicit Point( int n );

Point( const Point<T>& other );

Point( const std::vector<T>& coord );

~Point();

std::vector<T> Get_Coordinates() const;

void Set_Coordinates( const std::vector<T>& coord );

void Translate( const std::vector<T>& coord );

void Scale( const T& factor );

//unsigned int Distance( const Point<T>& other );

//unsigned int Distance_Origin()
void WriteLn();

private:
static const int Expt_Different_Sizes;

};

template <typename T>
Point<T>::Point( int n ):
coordinates( n )
{
}

template <typename T>
Point<T>::Point( const Point<T>& other ):
coordinates( other.coordinates )
{
}

template <typename T>
Point<T>::Point( const std::vector<T>& coord ):
coordinates( coord )
{
}

template <typename T>
Point<T>::~Point()
{
}

template <typename T>
void Point<T>::Set_Coordinates( const std::vector<T>& coord )
{
if( coordinates.size() != coord.size() )
throw Point<T>::Expt_Different_Sizes;
else
{
for( int i=0; i<coordinates.size(); i++ )
coordinates[i] = coord[i];
}
}

template <typename T>
std::vector<T> Get_Coordinates() const
{
return coordinates;
}

template <typename T>
void Point<T>::Translate( const std::vector<T>& coord )
{
if( coordinates.size() != coord.size() )
throw Point<T>::Expt_Different_Sizes;

// Add the two coordinates
for( int i = 0; i < coordinates.size(); ++i )
coordinates[i] = coordinates[i] + coord[i];
}

template <typename T>
void Point<T>::Scale( const T& factor )
{
if( factor == 0 )
throw Point<T>::Expt_Divide_By_Zero;
else
{
for( int i = 0; i< coordinates.size(); i++ )
coordinates[i] = coordinates[i]/factor;
}
}

template <typename T>
void Point<T>::WriteLn()
{
typename std::vector<T>::iterator pos;

for( pos = coordinates.begin(); pos < coordinates.end(); ++pos)
std::cout << *pos << " ";

std::cout << std::endl;
}

// Specify the exception code
template <typename T>
const int Point<T>::Expt_Different_Sizes = 1;

template <typename T>
const int Point<T>::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<T,
std::allocator<_CharT> > 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<T>::Expt_Divide_By_Zero'
is not
a static member of `class Point<T>'
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<T>::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

P: n/a
Pedro Sousa wrote:
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_
Names beginning with an underscore and a capital letter are reserved
by the implemenation. IOW, you can't use them. Why do you think you
need the leading underscore, anyway?

#include <vector>

template <typename T>
class Point{
private:
std::vector<T> coordinates;

public:
explicit Point( int n );

Point( const Point<T>& other );

Point( const std::vector<T>& coord );

~Point();

std::vector<T> Get_Coordinates() const;

void Set_Coordinates( const std::vector<T>& coord );

void Translate( const std::vector<T>& coord );

void Scale( const T& factor );

//unsigned int Distance( const Point<T>& other );

//unsigned int Distance_Origin()
void WriteLn();

private:
static const int Expt_Different_Sizes;

};

template <typename T>
Point<T>::Point( int n ):
coordinates( n )
{
}

template <typename T>
Point<T>::Point( const Point<T>& other ):
coordinates( other.coordinates )
{
}

template <typename T>
Point<T>::Point( const std::vector<T>& coord ):
coordinates( coord )
{
}

template <typename T>
Point<T>::~Point()
{
}

template <typename T>
void Point<T>::Set_Coordinates( const std::vector<T>& coord )
{
if( coordinates.size() != coord.size() )
throw Point<T>::Expt_Different_Sizes;
else
{
for( int i=0; i<coordinates.size(); i++ )
coordinates[i] = coord[i];
}
}
template <typename T>
std::vector<T> Get_Coordinates() const ^^^^
Name of the class is missing here. At least that's what your
compiler is telling you.
{
return coordinates;
}

[...]
#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<T,
std::allocator<_CharT> > Get_Coordinates()' cannot have `const'
method qualifier
You need to supply the name of the class for which you are defining
the member.
[..]
template <typename T>
void Point<T>::Translate( const std::vector<T>& coord )
{
if( coordinates.size() != coord.size() )
throw Point<T>::Expt_Different_Sizes;

// Add the two coordinates
for( int i = 0; i < coordinates.size(); ++i )
coordinates[i] = coordinates[i] + coord[i];
}

template <typename T>
void Point<T>::Scale( const T& factor )
{
if( factor == 0 )
throw Point<T>::Expt_Divide_By_Zero;
else
{
for( int i = 0; i< coordinates.size(); i++ )
coordinates[i] = coordinates[i]/factor;
}
}

template <typename T>
void Point<T>::WriteLn()
{
typename std::vector<T>::iterator pos;

for( pos = coordinates.begin(); pos < coordinates.end(); ++pos)
std::cout << *pos << " ";

std::cout << std::endl;
}

// Specify the exception code
template <typename T>
const int Point<T>::Expt_Different_Sizes = 1;

template <typename T>
const int Point<T>::Expt_Divide_By_Zero = 2;
I don't think you declared that member in the class. Look at the
class template definition.

#endif
nt<T>::Expt_Different_Sizes;
Huh? Is this just a typo? Please have a closer look at what you
give to your compiler. You know, GIGO...
// Add the two coordinates
for( int i = 0; i < coordinates.size(); ++i )
coordinates[i] = coordinates[i] + coord[i];
}

[..]

include/point.hpp:127: error: `const int
Point<T>::Expt_Divide_By_Zero' is not
a static member of `class Point<T>'
include/point.hpp:127: error: template definition of non-template
`const int
Point<T>::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.


What links? Just clean up your code, buddy.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 14 '06 #3

P: n/a
>
What links? Just clean up your code, buddy.

V


Their was some typos, it append when I was copying the code to the mail,
but now I got it right.

The error:

std::allocator<_CharT> > Get_Coordinates()' cannot have `const'
method qualifier

was because the missing of Point<T> in the function definition

template <typename T>
std::vector<T> Point<T>::Get_Coordinates() const
{
return coordinates;
}

The other 2 errors was because I wanted to throw some exceptions when I
some behavior append, so I was declaring the constants for the
exceptions identification.

I resolve to do it in another way. I used 'enum' to define the
exceptions names and them I can use it the same way.

But I have one more question. What I've declared in the class was static
variables, so how can I define their values in the header file?

Regards
Pedro Sousa

--
Posted via a free Usenet account from http://www.teranews.com

Jun 14 '06 #4

P: n/a
Pedro Sousa wrote:
Victor Bazarov wrote:

What links? Just clean up your code, buddy.

Funny.

[snip]
The other 2 errors was because I wanted to throw some exceptions when I
some behavior append, so I was declaring the constants for the
exceptions identification.

I resolve to do it in another way. I used 'enum' to define the
exceptions names and them I can use it the same way.

But I have one more question. What I've declared in the class was static
variables, so how can I define their values in the header file?


Generally, you should define a class (probably derived from
std::exception) to throw rather than throwing an int or enum. It can be
nested within your Point template, but you might or might not want
that. See this FAQ for an example:

http://www.parashift.com/c++-faq-lit....html#faq-17.6

Cheers! --M

Jun 14 '06 #5

P: n/a
Pedro Sousa wrote:
[..] What I've declared in the class was
static variables, so how can I define their values in the header file?


They are members of a template. That makes them templates. If you want
to give them a generic definition, you just write their definition like
you would for any other static member, and add 'template' to it:

template<class T> const int Point<T>::Expt_Blah_Blah = 42;

You could also initialise it in the class definition (at least try it,
I am not sure all compilers support that).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 14 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.