473,757 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(cons t 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 1409
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(cons t 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<st d::vector<T::it erator.
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...@com Acast.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(cons t 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.d udewrote:
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.d udewrote:
>MathStuf wrote:

[redacted]


>>Hmm...guess I should have looked exactly where it was getting mixed
up there. It's in the following code:
>>template<clas s 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...@com Acast.netwrote:
MathStuf wrote:
On Jun 1, 5:11 pm, red floyd <no.s...@here.d udewrote:
MathStuf wrote:
[redacted]
>Hmm...guess I should have looked exactly where it was getting mixed
up there. It's in the following code:
>template<cla ss 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<Tma trixrow_t;
typedef std::vector<mat rixrow_tmatrix_ t;

matrix_t matrix;
...

public:
void AddRow(const T &d = T())
{
for(matrix_t::i terator 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<cla ss 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<Tma trixrow_t;
typedef std::vector<mat rixrow_tmatrix_ t;

matrix_t matrix;
...

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

for (typename matrix_t::itera tor 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
2090
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 for really ugly code. Would a templating engine solve that? Does anyone have any suggestions about which one to use?
7
1261
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 XML/HTML) I don't need a framework (already have it), but just simple templating. The syntax I had in mind is something like that:
2
1578
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% or <MYusername/> would be replaced by the value. basically what the perl cookbook uses in a few examples iirc. however more and more we have to deal with more complex data. variable length lists of data. say for example a list of new products...
1
967
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 have not defined the methods). When I put everything in one file, it all works beautifully... any suggestions on how to remedy this problem? THanks,
1
1281
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 can edit HTML files without having to get involved with VS at all. http://www.codeproject.com/aspnet/page_templates.asp - this seems to be the best so far, if I can modify the code to use an external template file. Are there any other...
18
4972
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 overwhelmed by the plethora of different frameworks, templating engines, HTML generation tools etc that exist. After some thought I decided to leave the various frameworks aside for the
0
943
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 generating textual output. The key features are:
7
1623
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 doubt I am using it to its full extent but I want to learn more about, and compare and contrast, frameworks versus IDE versus templating systems and MOST IMPORTANTLY learn what is going to save me time. I know technically the IDE is the tool in...
5
2327
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 pursue. I don't need MVC at the moment, but I don't know that the templating engines require it either. Thank you.
10
1719
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 constructs with the HTML document itself. An opposite approach to this form of dynamic HTML production is called push-style templating, as coined by Terence Parr:
0
9487
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9297
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10069
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9904
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9884
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8736
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6556
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5324
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3828
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.