What is wrong with the following program?

P

PengYu.UT

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;
}
 
V

Victor Bazarov

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

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.
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;
}

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top