problem with passing an argument

Hello, I'm quite new to C++ and my problems may seem silly. Please help me with that:
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <string>
using namespace std;
class Okno {
protected:    
    int x1,y1,x2,y2;
    string nazwa;
public:   
};

class NieDesktop : public Okno{
protected:
    Okno& ojciec;   
private:
    NieDesktop(const NieDesktop&);  
public:
    NieDesktop(Okno& _ojciec) : ojciec(_ojciec){}
           
};

class OknoWlasciwe : public NieDesktop{                      
    OknoWlasciwe(const OknoWlasciwe&);  
public:
    OknoWlasciwe(Okno&,int,int,int,int,string);       
};
OknoWlasciwe::OknoWlasciwe(Okno& _ojciec,int _x1,int _y1,int _x2,int _y2,string _nazwa) 
:NieDesktop(_ojciec){
    x1=_x1;
    y1=_y1;
    x2=_x2;
    y2=_y2;
    nazwa=_nazwa;
}
class Desktop : public Okno{
private:      
    Desktop(const Desktop&);                
public:
    Desktop();
    void OtworzOkno(int,int,int,int,string);                    
};
Desktop pulpit(); //deklaracja pulpitu jako zmiennej globalnej
Desktop::Desktop(){
    nazwa="desktop";
    x1=y1=0;
    x2=y2=1000000;
}

void Desktop::OtworzOkno(int _x1,int _y1,int _x2,int _y2,string _nazwa){
Okno* r = new OknoWlasciwe(pulpit,_x1,_y1,_x2,_y2,_nazwa);
}

int main(){}


This is what the compiler says:
C:\Documents and Settings\dom\Pulpit\test.cpp In member function `void Desktop::OtworzOkno(int, int, int, int, std::string)':
48 C:\Documents and Settings\dom\Pulpit\test.cpp no matching function for call to `OknoWlasciwe::OknoWlasciwe(Desktop (&)(), int&, int&, int&, int&, std::string&)'
note C:\Documents and Settings\dom\Pulpit\test.cpp:26 candidates are: OknoWlasciwe::OknoWlasciwe(Okno&, int, int, int, int, std::string)
note C:\Documents and Settings\dom\Pulpit\test.cpp:26 OknoWlasciwe::OknoWlasciwe(const OknoWlasciwe&)

I have no idea what it's talking about. Especially, Why does it say that I'm calling function with int& arguments? and what's this Desktop (&)() thing?
In function:
1
2
3
void Desktop::OtworzOkno(int _x1,int _y1,int _x2,int _y2,string _nazwa){
Okno* r = new OknoWlasciwe(pulpit,_x1,_y1,_x2,_y2,_nazwa);
}

where is pulpit declared?
in the line 40. Sorry, I didn't change the Polish description to English. It's a global variable.
Try this on line 40:
 
Desktop pulpit;  // no parentheses 


http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=439

EDIT: Forget that link, it is too verbose without getting to the point! Here's the deal:

The compiler thinks that Desktop pulpit(); is a function pointer forward declaration. Later, in Desktop::OtworzOkno(), it was passed as a parameter to a constructor that expected an Okno reference.

This ambiguity remains for C compatibility and has been coined as "C++'s most vexing parse," by Scott Meyers in his book, Effective C++.
Last edited on
My God, I have no idea what you're talking about :D Function pointer? What is it? I thought I can have pointers to variables only... Thank you anyway.
But this makes me think of another part of my code:
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
class OknoWlasciwe : public NieDesktop{                          //klasa zwykłych okien
    ListaOkien kontrolki;
    ListaOkien podokna;
    OknoWlasciwe(const OknoWlasciwe&);  
public:
    OknoWlasciwe(Okno&,int,int,int,int,string);     
    char KliknijOkno(int,int);
    void Przesun(int, int);
    void UstawRozmiar(int, int);    
};
OknoWlasciwe::OknoWlasciwe(Okno& _ojciec,int _x1,int _y1,int _x2,int _y2,string _nazwa) 
:NieDesktop(_ojciec),kontrolki(),podokna(){
try{
    int szer = _ojciec.daj_x2()-_ojciec.daj_x1();
    int wys = _ojciec.daj_y2()-_ojciec.daj_y1();
    if(_x1>=_x2 || _y1>=_y2) throw "niedodatni rozmiar tworzonego okna.";
    if(_x2-_x1>=szer || _y2-_y1>=wys) throw "zbyt duzy rozmiar okna.";
    if(_x1<0 || _y1<0) throw "ujemna wartosc x1 lub x2.";
    if(!_nazwa.compare("Desktop")) throw "zastrzezona nazwa - desktop.";
}
catch (char *str){
    cout<<"Awaria: "<<str<<endl;
}  
    x1=_x1;
    y1=_y1;
    x2=_x2;
    y2=_y2;
    nazwa=_nazwa;
}


:NieDesktop(_ojciec),kontrolki(),podokna()
There is no such ambiguity here, right?

And this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main(int argc, char** argv) {

    ListaOkien zminimalizowane();
    cout << endl << "Podawaj polecenia. Zakoncz poleceniem koniec." <<endl;
    string polecenie;
  
    do{
        getline(cin,polecenie);
        Wykonaj(polecenie);
        WypiszWszystko();
    }    
    while (polecenie.compare("koniec"));
              
}

Should it be:
ListaOkien zminimalizowane; with no parentheses?
The compiler says nothing, so he just thinks I'm declaring a function and everything is alright for him?
moorecm is correct.

You have an object OknoWlasciwe that has a constructor
OknoWlasciwe(Okno&,int,int,int,int,string);

So the first argument has to be a OknoWlasciwe, but you're passing it pulpit, which is a Desktop.

The compiler's complaining about this. That's what
no matching function for call to `OknoWlasciwe::OknoWlasciwe(Desktop (&)(), int&, int&, int&, int&, std::string&)'
means.

It's looking for alternative matches, but can't find any either, that's what
candidates are: OknoWlasciwe::OknoWlasciwe(Okno&, int, int, int, int, std::string)
means.

It also can't generate a copy constructor because the base contains a reference, that's what
note C:\Documents and Settings\dom\Pulpit\test.cpp:26 OknoWlasciwe::OknoWlasciwe(const OknoWlasciwe&) ...
means.

Last edited on
Actually, I'm sorry, it's a forward declaration for a function not a function pointer. :)

This explanation should help:
1
2
3
4
5
6
7
8
9
class A { /*...*/ };
//...
A a;    // instantiate an object of type A, using the default constructor
A f();  // forward declaration of a fucntion f, that takes no parameters,
        // and returns an object of type A
//...
f();    // call the function that has been declared but not yet defined
//...
A f() { /*...*/ }


See (function pointers are at the bottom):
http://www.cplusplus.com/doc/tutorial/pointers/


no matching function for call to `OknoWlasciwe::OknoWlasciwe(Desktop (&)(), int&, int&, int&, int&, std::string&)'

The error is pointing out what it was given.

Should it be:
ListaOkien zminimalizowane; with no parentheses?
The compiler says nothing, so he just thinks I'm declaring a function and everything is alright for him?

Yes, to default-construct an object the parentheses must be omitted. Exactly, the compiler will not complain until you use the function in the wrong manor (such as, treating it like an object). Above, zminimalizowane is never used.
Last edited on
Thank you so much for your help! :)
Topic archived. No new replies allowed.