Sign in to follow this  

Operator [] overloading

This topic is 4250 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 else

T_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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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"

Share this post


Link to post
Share on other sites
Quote:
Original post by Marty666
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


Well.. I was thinking of doing something like the below, although in the end (atleast to me) it looks clearer when it's in the header:

// Header.h
#ifndef HEADER_H
#defube HEADER_H

template<class T>
class a {
public:
void f();
};

#define IMPLEMENT_HEADER_H
#include "Header.cpp"
#undef IMPLEMENT_HEADER_H

// Header.cpp
#ifdef IMPLEMENT_HEADER_H
// implement 'a'
#endif



Or, you can make sure that header.cpp just isn't compiled when you compile your project.

Share this post


Link to post
Share on other sites

This topic is 4250 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this