473,657 Members | 2,380 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>::mat rix
(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 8095
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
3281
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 and I want to put my .java files in /home/me/src and my class files in /home/me/bin. For now, I'm keeping the .java files in /home/me/src and, after I compile a class, I do "mv src/*class bin/" to transfer the newly generated .class files into...
5
2517
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. I am using the compiler like this: javac -classpath .. <filename.java> The source code of the classes follow. Dijkstra.java is the class that won't compile, because the compiler doesn't recgonise the Vertex and GraphMap classes, even...
29
3433
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. I'm wondering if something of such had been attempted in python, as in compiling X language into .pyc. I do not understand the schematics of .pyc files but I assume that they are the so called python bytecode files. Or is there any...
4
1265
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 { public: virtual void Coin(TurnStyleContextFSM *) {};
2
2114
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 my other includes then I get lots of errors like C2011: 'fd_set' : 'struct' type redefinition warning C4005: 'FD_CLR' : macro redefinition which I understand are due to the fact that windows.h is being included in another header file as well as...
1
1276
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 whole team has to update the dll. I am using CodeBehind in all my files. I also created a whole bunch of class files (e.g. common.vb) . All files are using the same Namespace. Here is a little sample:( I am getting an error "Type 'Portal.Common' is...
1
1575
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 within my ASP.NET page, I am told that "Type 'Class1' is not defined", I cannot seem to find out how I can use this class, that exists within the same project as my ASP.NET site. The class' code is:
2
3524
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 contains 2 project - TCUtils and the Main line program. I'm compiling TestCase using CSharpCodeProvider().CreateProvider and building an in memory assembly. The source file compiles clean. I then call CreateInstance on the generated assembly using...
7
1353
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 Acme.cs has to be in a dir called 'Acme'? (am assuming namespace is equiv to Java's packages; is this right?) at any rate I can't compile Acme.cs.. get errors (I'm compiling in
4
2548
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 "tokenizer" I'm compiling from my home directory: % javac tokenizer/TokenizerMain.java ./tokenizer/Tokenizer.java:5: 'class' or 'interface' expected package tokenizer; ^ tokenizer/TokenizerMain.java:2: cannot access tokenizer.Tokenizer bad class...
0
8394
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
8825
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...
1
8503
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
8605
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7327
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...
1
6164
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 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...
0
4152
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
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.