473,394 Members | 2,168 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Templating classes

I am working on a class that will be a matrix based on a vector of
vectors. I am having trouble with the following code causing errors on
compilation:

template<class Tclass MatrixBase
{
public:
MatrixBase()
{
width = 0;
height = 0;
matrix.clear();
}
MatrixBase(const unsigned w, const unsigned h, const T &d = T())
{
matrix.resize(w, std::vector<T>(h, d));
width = w;
height = h;
}

void AddRow(const T &d = T())
{
for (std::vector<T>::iterator i = matrix.begin(); i !=
matrix.end(); ++i)
i->push_back(d);
++height;
}
// More methods
protected:
std::vector< std::vector<T matrix;
unsigned height;
unsigned width;
};

In the AddRow method (and other, similar methods as well), it says
that it needs an ';' before i and that it's undeclared. It also says
that 'height' and 'width' are undeclared. Should I move the variable
definitions up to the top of the class or should the methods be
outside of it entirely?

Jun 1 '07 #1
9 1389
MathStuf wrote:
I am working on a class that will be a matrix based on a vector of
vectors. I am having trouble with the following code causing errors on
compilation:

template<class Tclass MatrixBase
{
public:
MatrixBase()
{
width = 0;
height = 0;
Those are better initialised.
matrix.clear();
No need to clear a freshly constructed vector.
}
MatrixBase(const unsigned w, const unsigned h, const T &d = T())
{
matrix.resize(w, std::vector<T>(h, d));
width = w;
height = h;
Again, those are better initialised than assigned.
}

void AddRow(const T &d = T())
{
for (std::vector<T>::iterator i = matrix.begin(); i !=
'matrix.begin()' returns 'std::vector<std::vector<T::iterator.
You probably want to make this loop nested.

Also, 'std::vector<T>::iterator' is a dependent name. The compiler
doesn't know that it can be used where a type is expected. You need
to tell the compiler to trust you:

for (typename std::vector<T>::iterator ...
^^^^^^^^
matrix.end(); ++i)
i->push_back(d);
If you don't make this loop nested (which is fine), you might want
to review what you're pushing.
++height;
}
// More methods
protected:
std::vector< std::vector<T matrix;
unsigned height;
unsigned width;
};

In the AddRow method (and other, similar methods as well), it says
that it needs an ';' before i and that it's undeclared.
That's because it doesn't believe that 'std::vector<T>::iterator' is
in fact a type. Use 'typename' to tell it.
It also says
that 'height' and 'width' are undeclared.
Where?
Should I move the variable
definitions up to the top of the class or should the methods be
outside of it entirely?
Not sure what you're asking here, sorry.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 1 '07 #2
On Jun 1, 4:39 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
MathStuf wrote:
I am working on a class that will be a matrix based on a vector of
vectors. I am having trouble with the following code causing errors on
compilation:
template<class Tclass MatrixBase
{
public:
MatrixBase()
{
width = 0;
height = 0;

Those are better initialised.
matrix.clear();

No need to clear a freshly constructed vector.
}
MatrixBase(const unsigned w, const unsigned h, const T &d = T())
{
matrix.resize(w, std::vector<T>(h, d));
width = w;
height = h;

Again, those are better initialised than assigned.
Ah, thanks.
That's because it doesn't believe that 'std::vector<T>::iterator' is
in fact a type. Use 'typename' to tell it.
It also says
that 'height' and 'width' are undeclared.

Where?
Hmm...guess I should have looked exactly where it was getting mixed up
there. It's in the following code:
template<class Tclass Matrix : public MatrixBase<T>
{
public:
Matrix()
{
width = 0;
height = 0;
matrix.clear();
}
// More methods
};

Since matrix, height, and width are protected, shouldn't they be
available to any class which inherits MatrixBase and invisible to
external interfaces? Or do I have it backwards?
Should I move the variable
definitions up to the top of the class or should the methods be
outside of it entirely?

Not sure what you're asking here, sorry.
I thought that the problem may have been matrix being defined after
the methods. It's a moot point now, since the typename fixed it.

Jun 1 '07 #3
MathStuf wrote:
>[redacted]

Hmm...guess I should have looked exactly where it was getting mixed up
there. It's in the following code:
template<class Tclass Matrix : public MatrixBase<T>
{
public:
Matrix()
{
width = 0;
height = 0;
matrix.clear();
}
// More methods
};

Since matrix, height, and width are protected, shouldn't they be
available to any class which inherits MatrixBase and invisible to
external interfaces? Or do I have it backwards?
I *THINK* this is one of the rare cases where you need to qualify the
member, i.e. either MatrixBase<T>::width, or this->width.
Jun 1 '07 #4
MathStuf wrote:
[redacted]
>
Hmm...guess I should have looked exactly where it was getting mixed up
there. It's in the following code:
template<class Tclass Matrix : public MatrixBase<T>
{
public:
Matrix()
{
width = 0;
height = 0;
matrix.clear();
}
// More methods
};

Note that since width and height are members of MatrixBase<>, it's
probably better to let MatrixBase set them. If you need something other
than what MatrixBase has set as the "default", MatrixBase should have a
constructor with width and height parameters.
Jun 1 '07 #5
On Jun 1, 5:11 pm, red floyd <no.s...@here.dudewrote:
MathStuf wrote:

[redacted]


Hmm...guess I should have looked exactly where it was getting mixed up
there. It's in the following code:
template<class Tclass Matrix : public MatrixBase<T>
{
public:
Matrix()
{
width = 0;
height = 0;
matrix.clear();
}
// More methods
};

Note that since width and height are members of MatrixBase<>, it's
probably better to let MatrixBase set them. If you need something other
than what MatrixBase has set as the "default", MatrixBase should have a
constructor with width and height parameters.
Alright, but I still have problems with their availability. I have
GetWidth() and GetHeight() methods in MatrixBase<>, but when i go to
call it, the compiler complains about it.

\Matrix.h:161: error: there are no arguments to `GetHeight' that
depend on a template parameter, so a declaration of `GetHeight' must
be available

I'm confused since MatrixBase<Tis inherited as public, and protected
members should be available (as well as public ones such as
GetHeight() and GetWidth()). I have other classes (non-template
though) where an id is inherited from the base class (public
inheritance of a protected variable) and straight calls to the id
works. Do templates mess with inheritance at all? I don't think they
would, but that's what it would seem is happening here.

Jun 1 '07 #6
MathStuf wrote:
On Jun 1, 5:11 pm, red floyd <no.s...@here.dudewrote:
>MathStuf wrote:

[redacted]


>>Hmm...guess I should have looked exactly where it was getting mixed
up there. It's in the following code:
>>template<class Tclass Matrix : public MatrixBase<T>
{
public:
Matrix()
{
width = 0;
height = 0;
matrix.clear();
}
// More methods
};

Note that since width and height are members of MatrixBase<>, it's
probably better to let MatrixBase set them. If you need something
other than what MatrixBase has set as the "default", MatrixBase
should have a constructor with width and height parameters.

Alright, but I still have problems with their availability. I have
GetWidth() and GetHeight() methods in MatrixBase<>, but when i go to
call it, the compiler complains about it.

\Matrix.h:161: error: there are no arguments to `GetHeight' that
depend on a template parameter, so a declaration of `GetHeight' must
be available
That's just not true. 'GetHeight' is non-static, so it *does* have
one argument that depends on the template argument - the instance of
the class (the object). But it doesn't really matter. The full name
of 'GetHeight' is 'MatrixBase<T>::GetHeight', which means it *does*
depend on 'T' (simply because it is a member of the template).
I'm confused since MatrixBase<Tis inherited as public, and protected
members should be available (as well as public ones such as
GetHeight() and GetWidth()). I have other classes (non-template
though)
That's *the* difference.
where an id is inherited from the base class (public
inheritance of a protected variable) and straight calls to the id
works. Do templates mess with inheritance at all?
No, but they do have slightly different rules for name lookup.
I don't think they
would, but that's what it would seem is happening here.
No, you just need to learn name lookup rules.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 1 '07 #7
On Jun 1, 5:46 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
MathStuf wrote:
On Jun 1, 5:11 pm, red floyd <no.s...@here.dudewrote:
MathStuf wrote:
[redacted]
>Hmm...guess I should have looked exactly where it was getting mixed
up there. It's in the following code:
>template<class Tclass Matrix : public MatrixBase<T>
{
public:
Matrix()
{
width = 0;
height = 0;
matrix.clear();
}
// More methods
};
Note that since width and height are members of MatrixBase<>, it's
probably better to let MatrixBase set them. If you need something
other than what MatrixBase has set as the "default", MatrixBase
should have a constructor with width and height parameters.
Alright, but I still have problems with their availability. I have
GetWidth() and GetHeight() methods in MatrixBase<>, but when i go to
call it, the compiler complains about it.
\Matrix.h:161: error: there are no arguments to `GetHeight' that
depend on a template parameter, so a declaration of `GetHeight' must
be available

That's just not true. 'GetHeight' is non-static, so it *does* have
one argument that depends on the template argument - the instance of
the class (the object). But it doesn't really matter. The full name
of 'GetHeight' is 'MatrixBase<T>::GetHeight', which means it *does*
depend on 'T' (simply because it is a member of the template).
Okay...I put MatrixBase<T>:: scoping on all references to height,
width, and matrix, and it seems to work fine now. Thanks for all the
help.

--MathStuf
Jun 1 '07 #8
>
Also, 'std::vector<T>::iterator' is a dependent name. The compiler
doesn't know that it can be used where a type is expected. You need
to tell the compiler to trust you:

for (typename std::vector<T>::iterator ...
^^^^^^^^
This is one of the reasons I like to use typedefs. It makes the code
easier to read and removes ambiguities. For example

template<class Tclass MatrixBase
{
protected:
typedef std::vector<Tmatrixrow_t;
typedef std::vector<matrixrow_tmatrix_t;

matrix_t matrix;
...

public:
void AddRow(const T &d = T())
{
for(matrix_t::iterator i = matrix.begin();
i != matrix.end(); ++i)
{
i->push_back(d);
}
++height;
}
...
};

Doing this would also make another error in the OP's code more obvious:
>template<class Tclass MatrixBase
{
public:
...
void AddRow(const T &d = T())
{
for (std::vector<T>::iterator i = matrix.begin(); i !=
matrix.end(); ++i)
i->push_back(d);
++height;
}
// More methods
protected:
std::vector< std::vector<T matrix;
unsigned height;
unsigned width;
};
matrix.begin() returns an iterator to a std::vector< std::vector<T,
not an iterator to a std::vector<T>.

Using typedefs, you'd probably notice that bug without ever even running
it through the compiler.

-- David
Jun 1 '07 #9
David C. wrote:
>Also, 'std::vector<T>::iterator' is a dependent name. The compiler
doesn't know that it can be used where a type is expected. You need
to tell the compiler to trust you:

for (typename std::vector<T>::iterator ...
^^^^^^^^

This is one of the reasons I like to use typedefs. It makes the code
easier to read and removes ambiguities. For example

template<class Tclass MatrixBase
{
protected:
typedef std::vector<Tmatrixrow_t;
typedef std::vector<matrixrow_tmatrix_t;

matrix_t matrix;
...

public:
void AddRow(const T &d = T())
{
for(matrix_t::iterator i = matrix.begin();
I believe you still have to use 'typename':

for (typename matrix_t::iterator i = matrix.begin();
i != matrix.end(); ++i)
{
i->push_back(d);
}
++height;
}
...
};

[..]

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
by: Leif K-Brooks | last post by:
I'm planning to start on a fairly large web application, most likely using mod_python. I recently wrote a fairly small (but real-world useful) web app with it, and all of those req.write()s made...
7
by: Ksenia Marasanova | last post by:
Hi, I am looking for fast, simple templating system that will allow me to do the following: - embed Python code (or some templating code) in the template - generate text output (not only...
2
by: lloyd christopher | last post by:
im developing a template based web application in perl and need some guidance. currently its all pretty simple stuff, we tell the designers place holders to use and then &username; or %USERNAME%...
1
by: JL | last post by:
When trying to template a class where the class declaration is in a header file and the class specification is in the source file, I receive linking errors (Unresolved External Symbols... as if I...
1
by: Chris Ashley | last post by:
I have been looking into templating systems these past few days, but nothing seems to suit my purpose. I want to achieve complete separation of code and design in a templating system, so a designer...
18
by: pkassianidis | last post by:
Hello everybody, I am in the process of writing my very first web application in Python, and I need a way to generate dynamic HTML pages with data from a database. I have to say I am...
0
by: Chris Withers | last post by:
Hi All, I'm proud to announce that after almost a year's development, the first public release of Twiddler is available! Twiddler is a simple but flexible templating system for dynamically...
7
by: Paul | last post by:
I have been coding apps with PHP for several years. But I am getting more involved in larger and more complicated PHP applications and want to use best practices. I use Eclipse's PHP IDE. I...
5
by: jmDesktop | last post by:
Hi, I was using .net and it uses templates. I like that it did not mix the UI with logic. I saw there are many templating engines for php usage. Can you recommend one? I don' t know which to...
10
by: Terrence Brannon | last post by:
Hello, The most common way of dynamically producing HTML is via template engines like genshi, cheetah, makotemplates, etc. These engines are 'inline' --- they intersperse programming...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.