By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,905 Members | 1,283 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,905 IT Pros & Developers. It's quick & easy.

What is wrong with the following program?

P: n/a
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;
}

Oct 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Pe*******@gmail.com wrote:
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
Oct 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.