Connecting Tech Pros Worldwide Forums | Help | Site Map

What is wrong with the following program?

PengYu.UT@gmail.com
Guest
 
Posts: n/a
#1: Oct 22 '05
I forward declared the class test. But I still get some error. Would
you please help me to find out what is wrong with this program?

Peng

//#include <utility>
#include <iostream>

//using namespace std::rel_ops;

class test;

class const_test{
public:
friend class test;
const_test(const test &t) : _i(t._i){}//error
const_test(int i) : _i(i){}
bool operator == (const const_test& t) const {
return _i == t._i;
}
private:
int _i;
};

class test{
public:
test(int i) : _i(i){}
bool operator == (const const_test& t) const {
return _i == t._i;
}
private:
int _i;
};

int main(){
test t1(1);
test t2(2);
// std::cout << (t1 != t2) << std::endl;
// std::cout << (t1 == t2) << std::endl;
}


Victor Bazarov
Guest
 
Posts: n/a
#2: Oct 22 '05

re: What is wrong with the following program?


PengYu.UT@gmail.com wrote:[color=blue]
> I forward declared the class test. But I still get some error. Would
> you please help me to find out what is wrong with this program?
>
> Peng
>
> //#include <utility>
> #include <iostream>
>
> //using namespace std::rel_ops;
>
> class test;
>
> class const_test{
> public:
> friend class test;
> const_test(const test &t) : _i(t._i){}//error[/color]

You're using what to the compiler seems to be a member of 'test',
before defining that class. The compiler cannot generate code
without knowing what 'test' looks like inside. You will need to
move the definition of this c-tor below the 'class test' definition.
[color=blue]
> const_test(int i) : _i(i){}
> bool operator == (const const_test& t) const {
> return _i == t._i;
> }
> private:
> int _i;
> };
>
> class test{
> public:
> test(int i) : _i(i){}
> bool operator == (const const_test& t) const {
> return _i == t._i;
> }
> private:
> int _i;
> };
>
> int main(){
> test t1(1);
> test t2(2);
> // std::cout << (t1 != t2) << std::endl;
> // std::cout << (t1 == t2) << std::endl;
> }[/color]

V


Closed Thread