473,503 Members | 2,107 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

compiling class-template member function

Hi, I am using visual studio.net and I am having problems using this bit
of code when I create an instance,

////////////////////////////////////////////
template<class T>
class matrix : public vector<T> {
public:

matrix(size_t h, size_t w) : vector(h*w), width(w) {}

reference operator()(int i, int j) { return (*this)[i*width+j]; }

const_reference operator()(int i, int j) const { return (*this)
[i*width+j]; }
protected:

size_t width;
};

//////////////////////////////////////////////

error C2955: 'std::vector' : use of class template requires template
argument list : see declaration of 'std::vector'
while compiling class-template member function 'matrix<T>::matrix
(size_t,size_t)'
with
[
T=Point
]
: see reference to class template instantiation 'matrix<T>' being
compiled
with
[
T=Point
]
error C2614: 'matrix<T>' : illegal member initialization: 'vector' is not
a base or member
with
[
T=Point
]
Jul 19 '05 #1
5 8083
Bruce Lee Roy wrote:
Hi, I am using visual studio.net and I am having problems using this
bit of code when I create an instance,

////////////////////////////////////////////
template<class T>
class matrix : public vector<T> {
public:

matrix(size_t h, size_t w) : vector(h*w), width(w) {}
You need to use a concrete type here, not a template-name:

matrix(size_t h, size_t w) : vector<T>(h*w), width(w) {}

Note the <T>
reference operator()(int i, int j) { return (*this)[i*width+j]; }

const_reference operator()(int i, int j) const { return (*this)
[i*width+j]; }
protected:

size_t width;


This is a call for disaster since you have made vector<T> a public base
class and std::vector has no virtual destructor.

Having said all that I suggest that in this case you make that vector a
member of your matrix class. There are many-many reasons to do that. One
of which is written above. The other reason is that in your code above you
have wondered to dependent-base-class-land and believe me you do not want to
go there unless you have to.

--
WW aka Attila
Jul 19 '05 #2
White Wolf wrote:
Bruce Lee Roy wrote:
.... This is a call for disaster since you have made vector<T> a public base
class and std::vector has no virtual destructor.


(OMG - the virtual destructor police are BAAACK.)

Wether interitance is better than containment for you I'll leave for
when/if we have a better idea of what you're trying to build.

Generally speaking however, it is perfectly fine to inherit from vector
even though it has no virtual destructor. You just need to make sure
that you allways delete from the most derived class which is only a
problem if you dynamically allocate the class and a fairly trivial rule
to keep.

(this is like one of those cases "Doctor doctor, my head hurts when I
bash it against the brick wall", doctor answers "Well stop bashing it
against a brick wall"...Profit)
Jul 19 '05 #3
Gianni Mariani wrote:
White Wolf wrote:
Bruce Lee Roy wrote:
...
This is a call for disaster since you have made vector<T> a public
base class and std::vector has no virtual destructor.


(OMG - the virtual destructor police are BAAACK.)

Wether interitance is better than containment for you I'll leave for
when/if we have a better idea of what you're trying to build.


Well, then you have a long reading ahead of you. Herb Sutter books
dedicated to C++: how inheritance instead of the proper composition (like
here) can make it impossible to create exception safe code.

Then the Design Patterns, where the ground rule #2 is: prefer composition
over inheritance.
Generally speaking however, it is perfectly fine to inherit from
vector even though it has no virtual destructor.
Generally speaking it is not. As soon as you add additional members, it is
not safe. Generally speaking you may feel it is perfectly fine to walk on
the 50th floor windowsill without safety belt, because you know what you are
doing. The trouble is not that. The trouble is that others will be there
too.
You just need to
make sure that you allways delete from the most derived class which
is only a problem if you dynamically allocate the class and a fairly
trivial rule to keep.
There is no way to ensure this in any decent sized project.

In the OPs case inheritance is completely wrong. Matrix is not a vector.
It is implemented in terms of a vector. Tomorrow he might want to change
this vector to something else (deque or whatever makes sense). Still his
design says that matrix is a kind of vector. This is plain not true.
(which is false ;-) )
(this is like one of those cases "Doctor doctor, my head hurts when I
bash it against the brick wall", doctor answers "Well stop bashing it
against a brick wall"...Profit)


I fail to see what this has to do with the OPs question and my answer. I
have given the solution to the OPs immediate problems and suggested taking t
he appropriate action to avoid future problems arising from his inferior
design. So while it might sound funny what you wrote, it has nothing to do
with this thread.

--
Attila aka WW
Jul 19 '05 #4
Attila Feher wrote:
Gianni Mariani wrote:
White Wolf wrote:
Bruce Lee Roy wrote:

...
This is a call for disaster since you have made vector<T> a public
base class and std::vector has no virtual destructor.


(OMG - the virtual destructor police are BAAACK.)

Wether interitance is better than containment for you I'll leave for
when/if we have a better idea of what you're trying to build.

Well, then you have a long reading ahead of you. Herb Sutter books
dedicated to C++: how inheritance instead of the proper composition (like
here) can make it impossible to create exception safe code.

Then the Design Patterns, where the ground rule #2 is: prefer composition
over inheritance.


Great. Another guy who makes a decision on incomplete information.

Fire Ready Aim.

I kindly suggest you take your discussion about inheritance to
comp.object, I'm sure the folks there would love to help you.
Generally speaking however, it is perfectly fine to inherit from
vector even though it has no virtual destructor.

Generally speaking it is not. As soon as you add additional members, it is
not safe. Generally speaking you may feel it is perfectly fine to walk on
the 50th floor windowsill without safety belt, because you know what you are
doing. The trouble is not that. The trouble is that others will be there
too.


Come back down to earth.

Give me some FACTS. When I need religion, I go to church.

If you have lazy ass coders, get them to write in perl.

You just need to
make sure that you allways delete from the most derived class which
is only a problem if you dynamically allocate the class and a fairly
trivial rule to keep.

There is no way to ensure this in any decent sized project.


Cool, and you have facts ? Go ahead an demonstrate instances where you
found problems because of this. I wait with baited breath.

....
(this is like one of those cases "Doctor doctor, my head hurts when I
bash it against the brick wall", doctor answers "Well stop bashing it
against a brick wall"...Profit)

I fail to see what this has to do with the OPs question and my answer.


Obviously.
Jul 19 '05 #5
Gianni Mariani wrote:
Then the Design Patterns, where the ground rule #2 is: prefer
composition over inheritance.
Great. Another guy who makes a decision on incomplete information.


All the information needed to know was there. of course if you do not have
the experiences you might not see it.
Fire Ready Aim.

I kindly suggest you take your discussion about inheritance to
comp.object, I'm sure the folks there would love to help you.
I kinfly suggest you learn basic C++ design and participate in real life
sized projects to understand how wrong you are.
Come back down to earth.

Give me some FACTS.
I gave you the facts. Take your time to look at them.
When I need religion, I go to church.
Good. Do so. And post about it to alt.religion
If you have lazy ass coders, get them to write in perl.


I can see you have a lot to learn about real life software projects. And
BTW getting personal does not help you to debate a technical topic.
There is no way to ensure this in any decent sized project.


Cool, and you have facts?
Go ahead an demonstrate instances where you
found problems because of this. I wait with baited breath.


Cool. Let's remember that it is much easier to prove something exists than
to prove it isn't. So let's make it all very simple for us. You give a
solution which portably guarantees a compilation error if someone happens to
try to delete the derived object via a pointer to base. Since "make it not
compilable" the *only* way to *ensure* it will never happen. I am awaiting
your magic solution.
(this is like one of those cases "Doctor doctor, my head hurts when
I
bash it against the brick wall", doctor answers "Well stop bashing
it against a brick wall"...Profit)

I fail to see what this has to do with the OPs question and my
answer.


Obviously.


Yes. Obvious, since it has nothing to do with it.

--
WW aka Attila
Jul 19 '05 #6

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

Similar topics

3
3268
by: Hal Vaughan | last post by:
My first Java project has gotten to the point where there are so many .java and .class files that I'd like to keep them separated so I can easily keep files straight. I have my /home/me directory...
5
2508
by: Herman | last post by:
Hi everyone, I'm implementing Dijkstra's algorithm for a class project, and I'm having trouble compiling the class that runs the algorithm. All of the other files compile fine except for this one....
29
3397
by: Maurice LING | last post by:
Hi, I remembered reading a MSc thesis about compiling Perl to Java bytecodes (as in java class files). At least, it seems that someone had compiled scheme to java class files quite successfully....
4
1256
by: ravinder | last post by:
#include <iostream.h> /* This class captures all the events as virtual functions. This class is a base class, and not one of the actual states of the FSM. */ class TurnStyleState {...
2
2100
by: Erik | last post by:
Hi Everyone, I'm having real problems compiling some source for eVC4++. The errors I am getting are below: It all seems to be centred around winsock. If I move the afsock.h reference to before...
1
1271
by: C Downey | last post by:
Is there a way to access my .vb class files in my codebehind pages without pre-compiling them first. We are working with multiple developers, so each time I make a change to the class file the...
1
1562
by: Paul Darroch | last post by:
Hello I am trying to develop a class, which I will only use within my ASP.NET site. I therefore created a class and inserted a simple test method in it. However, when I try to use this class...
2
3508
by: Ron James | last post by:
I'm trying to compile and load a C# source file "on the fly". The Source file contains a class (TestCase) that derives from TCBase. TCBase is defined in the TCUtils assembly. My Solution...
7
1347
by: maya | last post by:
hi, I'm following example 'Intro7.aspx' here, http://www.csharpfriends.com/quickstart/aspplus/doc/webformsintro.aspx#customctrls it says on top of Acme.cs "namespace Acme" I assume this means...
4
2541
by: betalpha | last post by:
I'm having trouble compiling my program in Unix. I initially developed it on Windows in Eclipse. I have two files: Tokenizer.java and TokenizerMain.java. They are both in a directory called...
0
7287
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,...
1
7011
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
7468
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...
1
5023
isladogs
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...
0
4689
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...
0
3180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3170
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1521
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 ...
1
747
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.