error: no match for call to ‘(Vector) (int&)’

Hey,
I have a problem with my code and I don't know how I can I fix it, because my skills with refeverences and pointer are not very well.

I have following three files:
1.) net.hpp:
#ifndef NET
#define NET
#include <cmath>
#include <cstdlib>
#include <cassert>

class Net;

class Vector {
private:
int dim;
double* coeff;
public:
Vector();
Vector(int dim);
Vector(int dim, double init);
~Vector();
int getDim();
void set(int k, double value);
double get(int k);
};
class Net {
private:
int inputPixel;
int outputPixel;
int hiddenPixel;
Vector inputSchicht;
Vector hiddenSchicht;
Vector outputSchicht;
public:
Net();
Net(int inputPixel, int outputPixel);
};
#endif

****************************************************************************

2.) net.cpp:
#include "net.hpp"
#include <iostream>
#include <cmath>

using std::cout;

Vector::Vector() {
dim = 0;
coeff = NULL;
}
Vector::Vector(int dim) {
int j = 0;
this->dim = dim;
coeff = (double*) malloc(dim*sizeof(double));
for (j=0; j<dim; ++j) {
coeff[j] = 0;
}
}
Vector::Vector(int dim, double init) {
int j = 0;
this->dim = dim;
coeff = (double*) malloc(dim*sizeof(double));
for (j=0; j<dim; ++j) {
coeff[j] = init;
}
}
Vector::~Vector() {
if (dim > 0) {
free(coeff);
}
// just for demonstration purposes
cout << "free vector, length " << dim << "\n";
}
int Vector::getDim() {
return dim;
}
void Vector::set(int k, double value) {
assert(k>=0 && k<dim);
coeff[k] = value;
}
double Vector::get(int k) {
assert(k>=0 && k<dim);
return coeff[k];
}
Net::Net() {
inputPixel = 0;
outputPixel = 0;
hiddenPixel = 0;
inputSchicht;
hiddenSchicht;
outputSchicht;

cout << "Default-Net wurde erstellt!" << "\n";
}
Net::Net(int intputPixel, int outputPixel){

this->inputPixel = inputPixel;

cout << "inputPixel: " <<inputPixel << "\n";

this->outputPixel = outputPixel;

inputSchicht(inputPixel);
//outputSchicht(outputPixel);

cout << "Net(" << inputPixel << "," << sqrt(inputPixel) << "x" << sqrt(inputPixel) << "," << inputPixel << ") wurde erstellt!" << "\n";
}

***************************************************************************

3.) knn.cpp:
#include "net.hpp"
#include <string>
#include <iostream>
#include <fstream>

using std::cout;
using std::string;

int main() {
Net net1;
Net net2(16,9);

return 0;

}

***************************************************************************

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 :)
inputSchicht(inputPixel);

inputSchnict is an object of type Vector. What are you trying to do here? It makes no sense.
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.

Here is how to do that:

Vector inputSchnict(inputPixel);
Yes I tryed that too.

I wrote:
Net::Net(int intputPixel, int outputPixel){

this->inputPixel = inputPixel;

cout << "inputPixel: " <<inputPixel << "\n";

this->outputPixel = outputPixel;

Vector inputSchicht(inputPixel);
//outputSchicht(outputPixel);

cout << "Net(" << inputPixel << "," << sqrt(inputPixel) << "x" << sqrt(inputPixel) << "," << inputPixel << ") wurde erstellt!" << "\n";
}

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.
And the answer of

"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.
1
2
3
4
5
6
7
8
9
10
11
12
13
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
Last edited on
First, do look up how to use code tags in posts.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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;
}


What you do need to use is class member initializer list syntax.
See http://en.cppreference.com/w/cpp/language/initializer_list
1
2
3
4
5
6
7
8
9
10
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.
Hey,
thanks for the quick solutions but I tried it and I got the same output:

inputPixel: 6303184
Net(6303184,2510.61x2510.61,6303184) wurde erstellt!

Do I have to change something in the net.hpp file? Because I changed only the constructor of Net in the net.cpp file.
show your updated code.
¿did you fix the typo?
What are you mean with typo?

knn.cpp:
#include "net.hpp"
#include <string>
#include <iostream>
#include <fstream>

int main() {

Net net1;
Net net2(16,9);

return 0;

}

**************************************************************************

net.hpp:
#ifndef NET
#define NET

#include <cmath>
#include <cstdlib>
#include <cassert>

class Net;

class Vector {
private:
int dim;
double* coeff;
public:
Vector();
Vector(int dim);
Vector(int dim, double init);
~Vector();
int getDim();
void set(int k, double value);
double get(int k);
};
class Net {
private:
int inputPixel;
int outputPixel;
int hiddenPixel;
Vector inputSchicht;
Vector hiddenSchicht;
Vector outputSchicht;

public:
Net();
Net(int inputPixel, int outputPixel);

};

#endif

****************************************************************************

net.cpp:
#include "net.hpp"
#include <iostream>
#include <cmath>

using std::cout;

Vector::Vector() {
dim = 0;
coeff = NULL;
}
Vector::Vector(int dim) {
int j = 0;
this->dim = dim;
coeff = (double*) malloc(dim*sizeof(double));
for (j=0; j<dim; ++j) {
coeff[j] = 0;
}
}
Vector::Vector(int dim, double init) {
int j = 0;
this->dim = dim;
coeff = (double*) malloc(dim*sizeof(double));
for (j=0; j<dim; ++j) {
coeff[j] = init;
}
}
Vector::~Vector() {
if (dim > 0) {
free(coeff);
}
// just for demonstration purposes
cout << "free vector, length " << dim << "\n";
}
int Vector::getDim() {
return dim;
}
void Vector::set(int k, double value) {
assert(k>=0 && k<dim);
coeff[k] = value;
}
double Vector::get(int k) {
assert(k>=0 && k<dim);
return coeff[k];
}
Net::Net() {
inputPixel = 0;
outputPixel = 0;
hiddenPixel = 0;

cout << "Default-Net wurde erstellt!" << "\n";
}
Net::Net(int intputPixel, int outputPixel)
:
inputPixel(inputPixel),
hiddenPixel (0),
outputPixel(outputPixel),
inputSchicht(inputPixel),
hiddenSchicht (0),
outputSchicht(outputPixel)
{
cout << "inputPixel: " << inputPixel << "\n";
cout << "Net(" << inputPixel << "," << sqrt(inputPixel) << "x" << sqrt(inputPixel) << "," << inputPixel << ") wurde erstellt!" << "\n";
}
Typo \Ty"po\, n. [An abbreviation of typographical error.]
     a typographical error; an error in typing, printing, etc.

I marked it already
Net::Net(int intputPixel, int outputPixel)
change intput to input.
Ah okay... a syntax error. Now I've seen it.
I changed intput to input and now it works.
Thank you all for your help :)
Topic archived. No new replies allowed.