Howdy,
I got the templatized class Matrix working in the way
it's written in the faq [16.18] , and it works fine as:
Matrix<sqr_T> display(80,25);
However, I'd like to have this variable in the private
section of another class, and I'd like to instantiate in
the class's constructor, with the option of having a size
determined at run-time.
Currently, my class looks like this:
class UI {
public:
UI();
~UI();
//etc
private:
sqr_T display[80][25];
// etc
}
I know I need to add an UI(short, short), but that's
the easy part -- I couldn't figure out the proper
syntax to include the templatized version of display.
Any idea?
TIA 1 1462
aurgathor wrote: Howdy,
I got the templatized class Matrix working in the way it's written in the faq [16.18] , and it works fine as: Matrix<sqr_T> display(80,25);
However, I'd like to have this variable in the private section of another class, and I'd like to instantiate in the class's constructor, with the option of having a size determined at run-time.
Currently, my class looks like this:
class UI { public: UI(); ~UI(); //etc private: sqr_T display[80][25]; // etc }
I know I need to add an UI(short, short), but that's the easy part -- I couldn't figure out the proper syntax to include the templatized version of display.
Any idea?
TIA
To add a templatized variable into a class is the same as creating an
automatic variable in a function.
private:
Matrix<sqr_T> display;
Now, to initialize it with the values 80 and 25, just use the
constructor initialization list and pass the variables just as if you
were passing them on declaration.
UI() :
display(80, 25)
{
.....
That is, unless you wanted the size to be determined by the
constructor, in which case your presupposition was correct, add a
constructor for UI that takes two doubles (or whatever the types are
for Matrix's constructor) and pass those to it in the initialization
list. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: hall |
last post by:
I have a problem with my design of a templatized class. I'm trying to
figure out how to load and save the data inside it, but can't. My class
looks like this
------------------------------------
template <class tType> class Foo {
private:
tType data;
|
by: Toke H?iland-J?rgensen |
last post by:
Hello. I am quite new to the c++ language, and am still trying to
learn it. I recently discovered how using include files would allow me
to split up my code into smaller segments, instead of having class
definitions etc. in one big file (yay, major discovery...).
My problem is this:
When I define a class in one include file, and then try to instantiate
it in another, I get compile-time errors saying the type is invalid.
If i move the...
|
by: mrstephengross |
last post by:
Ok, I've got code that looks something like this:
==================================================
template<typename T1, typename T2>
class Base
{
public:
explicit Base(const T1 & t1) { /* ... */ }
};
|
by: Joseph Turian |
last post by:
In a class templatized by class T, I have the following two lines:
hash_map<string, T> foo;
hash_map<string, T>::const_iterator p;
The first line compiles just fine.
The second line gives the following error: "syntax error before `;'
token"
Any idea how to resolve this error?
|
by: Ben Taylor |
last post by:
I've asked about includes before in this group in order to
stop files being #included either twice or not at all, and
somebody said to use extern keyword for global variables
that need to be used in another cpp file, and to also put
all the includes of header files in the stdafx.h file, and
then to have every cpp file just include stdafx and
nothing else.
I've done this, so far with success.
However now I want to create a class A, that...
| |
by: Bob Altman |
last post by:
Hi all,
This is the first time I've tried to create a templatized function, and I
can't get past a linker error. I create a new, unmanaged C++ application
and added a class named MyClass. In MyClass.h I added:
template <class T> void MyFunc(T);
In MyClass.cpp I added:
|
by: mark.moore |
last post by:
I know this has been asked before, but I just can't find the answer in
the sea of hits...
How do you forward declare a class that is *not* paramaterized, but is
based on a template class?
Here's what I thought should work, but apparently doesn't:
class Foo;
void f1(Foo* p)
|
by: tavianator |
last post by:
If I had a class with a templatized constructor, like this:
class foo
{
public:
template<typename T> foo();
};
|
by: Zachary Turner |
last post by:
Is it possible to declare a class with a templatized constructor? I
was sure it was, but GCC is giving me errors when I try to do this.
Most likely user error. Some example code is:
class MessageHeader
{
private:
int m_count;
public:
|
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...
|
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,...
| |
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...
|
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...
|
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,...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |