is this a bug with my IDE?

This compiles:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class myclass
{
private:
    int number;

public:
    myclass(int x) : number(x) {};
    int value() {return number;}
};

myclass test()
{
    return myclass(0);
}


This compiles:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class myclass
{
private:
    int number;

public:
    myclass(int x) : number(x) {};
    myclass(myclass &other) : number(other.value()) {};
    int value() {return number;}
};

myclass test()
{
    myclass result(0);
    return result;
}


This doesn't compile:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class myclass
{
private:
    int number;

public:
    myclass(int x) : number(x) {};
    myclass(myclass &other) : number(other.value()) {};
    int value() {return number;}
};

myclass test()
{
    return myclass(0);
}


Error message is "no matching function for call to 'myclass::myclass(myclass)".
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class myclass
{
private:
    int number;

public:
    myclass(int x) : number(x) {};
   
    // *** make these const-correct
    myclass(  const /*** added ***/ myclass &other) : number(other.value()) {};

    int value() const /*** added ***/ {return number;}
};

myclass test()
{
    return myclass(0);
}
const-correctness
You are trying to bind a non-const reference to a temporary.
Thanks, it worked. I will read up on this.
Topic archived. No new replies allowed.