Okay, my problem is that, if I compile the net.cpp file with g++ -c net.cpp, I always get the error:
net.cpp: In constructor ‘Net::Net(int, int)’:
net.cpp:145:25: error: no match for call to ‘(Vector) (int&)’
inputSchicht(inputPixel);
^
I tryed many things to fix this problem but it dosn't work. :-/
So please, can someone help me and send me the answer why my code doesn't work? This would be very nice :)
Hey :)
Yes I know. The elements of net are object of type Vector.
For example:
With the command Net net2(16,9), I want create two vectors (inputSchicht and outputschicht) of type Vector (dimension 16 and 9).
The constructor of Net should call the constructor of Vector to create the Vectors.
Do you know what I mean?
I think you're trying to create an object of type Vector, named inputSchnict, using the Vector constructor Vector::Vector(int dim), with the int value inputPixel as the parameter.
After that i didn't get a error but also not the correct solution, because if I started the programm with ./knn I got following output:
Default-Net wurde erstellt!
inputPixel: 6303184
Net(6303184,2510.61x2510.61,6303184) wurde erstellt!
free vector, length 6303184
Done
free vector, length 0
and I don't know why. I call the constructor of Net with the inputs int 16 and int 9 but the output of cout << inputPixel << "\n"; is 6303184. I believe any reference or pointer are wrong but I don't know too, why the inputs of the constructor of Net are reference or pointer and not int variables.
"I think you're trying to create an object of type Vector, named inputSchnict, using the Vector constructor Vector::Vector(int dim), with the int value
inputPixel as the parameter."
is YES! This is wath I'm trying.
But I want to creat some objects of type Vector with the constructor of Net.
Net::Net(int inputPixel, int outputPixel)
: //<- note, a colon
//this is called the initialization list
//here you'll construct/initialise all your member variables, and your parent
inputSchnict(inputPixel),
outputSchicht(outputPixel),
inputPixel(inputPixel),
outputPixel(outputPixel)
{ //<-note, open brace
//here all member variables are already constructed/initialised
cout << "inputPixel: " <<inputPixel << "\n";
cout << "Net(" << inputPixel << "," << sqrt(inputPixel) << "x" << sqrt(inputPixel) << "," << inputPixel << ") wurde erstellt!" << "\n";
}
About your output.
You've got a typo Net(int intputPixel, int outputPixel)
intput -> input
class Net {
private:
int inputPixel;
int outputPixel;
int hiddenPixel;
Vector inputSchicht;
Vector hiddenSchicht;
Vector outputSchicht;
public:
Net();
Net( int inputPixel, int outputPixel );
};
Net::Net() {
inputPixel = 0; // assignment
outputPixel = 0; // assignment
hiddenPixel = 0; // assignment
inputSchicht; // this statement does nothing
hiddenSchicht; // this statement does nothing
outputSchicht; // this statement does nothing
cout << "Default-Net wurde erstellt!" << "\n";
}
Net::Net( int intputPixel, int outputPixel ) {
this->inputPixel = inputPixel; // assignment
cout << "inputPixel: " <<inputPixel << "\n";
this->outputPixel = outputPixel; // assignment
inputSchicht(inputPixel); // error
cout << "Net(" << inputPixel << ","
<< sqrt(inputPixel) << "x" << sqrt(inputPixel)
<< "," << inputPixel << ") wurde erstellt!" << "\n";
}
The error is due to you using object like it were a function. That in itself is legal and used. Look for terms function object and functor. Alas, your Vector is not a functor; there is no Vector::operator() ( int ).
This code creates the exact same situation:
1 2 3 4 5 6 7
int main() {
int foo; // construction
Vector bar; // construction
foo = 42; // assignment
bar( foo ); // error
return 0;
}
Why do I mention construction and assignment?
In class constructor the constructors of members complete before the body of the constructor starts. By the time of your statements in Net constructor's body it is too late to try to call Vector::Vector(int).
1 2 3 4 5
int main() {
int foo = 42; // construction with parameter that initializes
Vector bar( foo ); // construction with parameter
return 0;
}
Net::Net( int intputPixel, int outputPixel )
: inputPixel( inputPixel ), // construction with parameter
outputPixel( outputPixel ), // construction with parameter
inputSchicht( inputPixel ) // construction with parameter
{
cout << "inputPixel: " <<inputPixel << "\n";
cout << "Net(" << inputPixel << ","
<< sqrt(inputPixel) << "x" << sqrt(inputPixel)
<< "," << inputPixel << ") wurde erstellt!" << "\n";
}
What, no this-> ? Yes, it is legal, but confusing. Foo::Foo( int X ) : Y( X ) {}
Within the initializer list the Y refers to a member Foo::Y, but the parameter it gets, X, is argument of the constructor (or another member of Foo, or global variable). Thus, the identifiers can be the same.