Operator [] overloading

Started by
9 comments, last by nullsquared 17 years, 12 months ago
Hi all, I was wondering how i could overload the [] operator to make it possible to do this, like in an array:

somevar = myclass[4];
and this:

myclass[4] = somevar;
can someone give me an example? Thanx! Marty
_____ /____ /|| | || MtY | ||_____|/Marty
Advertisement
class Stuff{public:   float& operator[](int a)   {      return actualdata[a];   }private:   float actualdata[a];};

Also check out http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.10
can it be done like this?

float& myclass::operator [] (unsigned int i);{    return &myvars;}


thanx,
Marty
_____ /____ /|| | || MtY | ||_____|/Marty
Quote:Original post by NickGravelyn
*** Source Snippet Removed ***


Thanx Nick,

but is it still possible to do:
SomeStuff s;SomeStuff[3] = 1.0f;


Or does it have to be a reference?

Marty
_____ /____ /|| | || MtY | ||_____|/Marty
You want to make sure the return type is a reference so you can do assigning values.

You want it to be
float& operator[](int a)

but in the function don't do
return &data[a]
just do
return data[a].

Make sense?

In your example you are using the class name with the operator, which you can't do, AFAIK. You could do s[4] = 1.0.
I get it, thanx Nick!

Another question:

I've got a template class:
template<unsigned int NUM_INPUTS, unsigned int NUM_HIDDEN, unsigned int NUM_OUTPUTS>class nnet{public:    nnet();    // mutates the network	void mutate(float rate);	// backpropagates one case through the network and returns the average output error	float backprop(float inputs[NUM_INPUTS], float correctinput[NUM_INPUTS], float learningrate);	// forward propagates one case through the network.    void forwardprop();    // holds the inputs    float inputs[NUM_INPUTS];	// holds the output values	float outputs[NUM_OUTPUTS];private:    // holds the values of the hidden neurons	float hidden[NUM_HIDDEN]; 	// sigmoid function    float sigmoid(float x);    // deridative of sigmoid function    float dsigmoid(float x);	// holds the connection weights from input- to hidden neurons    float icon[NUM_INPUTS][NUM_HIDDEN];    // holds the connection weights from hidden- to output neurons    float hcon[NUM_HIDDEN][NUM_OUTPUTS];    // generates random float between -1 and 1    float myrandom();};


Somewhere else i have this code:

typedef nnet<65, 6, 3> T_MAINNET;// and somewhere elseT_MAINNET mainnet;mainnet.feedforward();


I get an error saying:
... .objs\martybot.o:martybot.cpp:(.text+0x7e): undefined reference to `nnet<65u, 6u, 3u>::forwardprop()'collect2: ld returned 1 exit status


I have no clue what's wrong... Can anyone help out?

Thanx!
Marty
_____ /____ /|| | || MtY | ||_____|/Marty
Either you never wrote the function or you wrote it, but it never got included in the file in which it's referenced. I'll skip the gory details, but you need to have the function definition available when called, i.e. treat them just like inline functions. That's not quite true, but any other advice is more complex and you'd still end up treating them that way.
Hi,

I have 5 files:

main.cpp
nnet.h
nnet.cpp
martybot.h
martybot.cpp

nnet.cpp includes nnet.h and implements the neuralnet class
martybot.h includes nnet.h (which should be enough, right?)

in martybot.cpp the error occured. It includes martybot.h, so it should automatically include nnet.h aswell....

should all the implementation of a template class be in the same file as the definition???

thanx for the reply,
Marty
_____ /____ /|| | || MtY | ||_____|/Marty
Hi,

I copy pasted the contents of nnet.cpp (the implementation of the neural net class) to nnet.h.... It works now, although I'd rather have the definition and implementation in different files.

Is this possible?

Thanx,
Marty
_____ /____ /|| | || MtY | ||_____|/Marty
When you have a templated class, its implementation has to be in the header file also. But you could still have the implementation in another file. Here's an example:

// nnet.templ
// put implementation here

// nnet.h
// put definitions here

// include the template file
#include "nnet.templ"

This topic is closed to new replies.

Advertisement