473,396 Members | 1,970 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,396 software developers and data experts.

subclassing std::valarray

Hi,

since I want to specify an extra function for a std::valarray<float>, I want
to subclass it:

class FVector : public std::valarray<float> {
public:
FVector() : std::valarray<float>() {}
FVector(size_t size) : std::valarray<float>(size) {}
FVector(const FVector & other) : std::valarray<float>(other) {}
FVector(const float& f, size_t size) : std::valarray<float>(f, size
{}
FVector(const float * f, size_t size) : std::valarray<float>(f,
size) {}

float getDistance(const FVector & other) const;
};

I used to use a typedef for my FVector, but now I need the getDistance
method.

If I compile this however with gcc-3.4.4, I get the following error:

g++ -O2 -g -fno-inline -Wall -pipe -fPIC -Ibuild -Isrc -Isrc -c -o
build/Matrix.os src/Matrix.cpp
g++ -O2 -g -fno-inline -Wall -pipe -fPIC -Ibuild -Isrc -Isrc -c -o
build/NeuralNetwork.os src/NeuralNetwork.cpp
src/NeuralNetwork.cpp: In member function `FVector
NeuralNetwork::evalNetwork(const FVector&) const':
src/NeuralNetwork.cpp:168: error: conversion from
`std::_Expr<std::_BinClos<std::__plus, std::_ValArray, std::_ValArray,
float, float>, float>' to non-scalar type `FVector' requested
src/NeuralNetwork.cpp:170: error: conversion from
`std::_Expr<std::_ValFunClos<std::_ValArray, float>, float>' to non-scalar
type `FVector' requested
src/NeuralNetwork.cpp:172: error: conversion from
`std::_Expr<std::_BinClos<std::__plus, std::_ValArray, std::_ValArray,
float, float>, float>' to non-scalar type `FVector' requested
src/NeuralNetwork.cpp:174: error: conversion from
`std::_Expr<std::_ValFunClos<std::_ValArray, float>, float>' to non-scalar
type `FVector' requested
scons: *** [build/NeuralNetwork.os] Error 1
scons: building terminated because of errors.

As you see it complains on the operators:

168 FVector hidInput = getInp2Hid() * input +
getHiddenBiasWeights();
169 // Calculate the output of the hidden layer.
170 FVector hidOutput = hidInput.apply(intervalMapFunction);
171 // Calculate the input in the output layer.
172 FVector outpInput = getHid2Outp() * hidOutput +
getOutputBiasWeights();
173 // Calculate the output of the output layer.
174 FVector outpOutput = outpInput.apply(intervalMapFunction);
Do the operators not get inherited properly and should I redefine them all,
just like with the constructors (*sigh*)?

Regards,

Jan
Jan 5 '06 #1
2 2385
Jan Callewaert wrote:
since I want to specify an extra function for a std::valarray<float>, I want
to subclass it:

class FVector : public std::valarray<float> {
public:
float getDistance(const FVector & other) const;
};

I used to use a typedef for my FVector, but now I need the getDistance
method.
Actually, it might be better to have 'getDistance' a non-member function
that takes two arguments... It has no business of being a member, it
does not change its object.

If I compile this however with gcc-3.4.4, I get the following error:

[...]
src/NeuralNetwork.cpp:172: error: conversion from
`std::_Expr<std::_BinClos<std::__plus, std::_ValArray, std::_ValArray,
float, float>, float>' to non-scalar type `FVector' requested
[..]
As you see it complains on the operators:
[..]
172 FVector outpInput = getHid2Outp() * hidOutput +
getOutputBiasWeights();
[...]
Do the operators not get inherited properly and should I redefine them all,
just like with the constructors (*sigh*)?


No, but they return 'valarray', not FVector. What you need is
a constructor for FVector _from_ a std::valarray<float>:

FVector(std::valarray<float> const& a) : std::valarray<float>(a) {}

but you definitely have no justification to create 'FVector' in the first
place. A simple function

float getDistance(std::valarray<float> const& a1,
std::valarray<float> const& a2)
{
// whatever
}

should suffice.

V
Jan 5 '06 #2
Jan Callewaert wrote:
since I want to specify an extra function for a std::valarray<float>,
Then specify an extra function! This is what non-member functions are
for.
I want to subclass it:


This is a bad idea! First of all, it is not necessary and second,
'std::valarray' is clearly not intended to be derived from as is
indicated by several aspects:
- It has no virtual functions.
- 'std::valarray' is a value type, not a reference type.

Subclassing is only appropriate in relatively few situations. Using
non-member functions or non-member function templates is the right
way to add functionality to a class. In fact, I would claim that most
standard C++ library classes are currently already unreasonably bloated
with function which had better been non-member functions.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Jan 6 '06 #3

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

Similar topics

6
by: Christian Brechbühler | last post by:
The template std::valarray behaves pretty much like a mathematical vector. Arithmetic operators apply elementwise. Now I'd like to extend this to a user-defined type, e.g., complex. ...
0
by: Prune Tracy | last post by:
I have a question about assignment of slices of valarrays. I want to assign one general slice of a valarray to another. It seems the only way is to use the static_cast syntax, but this seems to...
6
by: Steven T. Hatton | last post by:
I bought Josuttis's book on the repeated recommendations of people in this newsgroup. http://www.josuttis.com/libbook/ One of the first things I looked up was the std::valarray<>. And what I...
2
by: Michael Hopkins | last post by:
Hi all I have a subclass of valarray<T> thus template <typename T> class uo_val : public std::valarray<T> { public: uo_val ( ) : std::valarray<T>() {} uo_val (const int sz ) :...
1
by: Oliver Block | last post by:
I have a (9x9) valarray std::valarray<int> va(81); va = 0; // fil all elements with zeros I am taking row slices like that: std::slice_array<int> slr = va; // r = row; a value between 1...
1
by: Dack | last post by:
Hi, I want to track memory leaks in my application (that is using <valarray>). I used the following code: #define _CRTDBG_MAP_ALLOC #include <stdlib.h> #include <crtdbg.h> But then, when I...
9
by: Jim | last post by:
Hi, I want to declare that that a valarray of a certain name exist at the beginning of some code, but I can't instatiate it until I've read in some parameters later on in a for loop i.e. int...
5
by: Chris Forone | last post by:
Hello group, g++ (3.4.2, mingw): float init = {1.f, 2.f, 3.f}; std::map<std::string, std::valarray<float mp; mp = std::valarray(init, 3); mp.size(); // should be 3, but IS 0!
43
by: john | last post by:
Hi, in TC++PL 3 on pages 674-675 it is mentioned: "Maybe your first idea for a two-dimensional vector was something like this: class Matrix { valarray< valarray<doublev; public: // ... };
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
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
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...
0
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,...

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.