🔒 Locked
Operator [] overloading
Advertisement
Original Post
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
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.
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:
Somewhere else i have this code:
I get an error saying:
I have no clue what's wrong... Can anyone help out?
Thanx!
Marty
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 statusI have no clue what's wrong... Can anyone help out?
Thanx!
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
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
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"
// nnet.templ
// put implementation here
// nnet.h
// put definitions here
// include the template file
#include "nnet.templ"
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_Htemplate<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'#endifOr, you can make sure that header.cpp just isn't compiled when you compile your project.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement