'access private member' error

A

AngreGanon

Hi, all~ ^^

If i use #1 OK, but use # 2 error happend what's different?

#include <iostream>
/* -----------# 1------------
using std::endl;
using std::cout;
using std::eek:stream;
*/------------------------------

//-------------# 2--------------
using namespace std; //error!!! ' x' : cannot access private member
declared in class 'Point'

class Point
{
private:
int x;
public:
Point(int _x=0):x(_x){}
friend ostream& operator<<(ostream& os, const Point& p);
};

ostream& operator<<(ostream& os, const Point& p)
{
os<<"["<<p.x<<"]"<<endl;
return os;
}

int main(void)
{
Point p;
cout << p;
return 0;
}
 
I

Ian Collins

Hi, all~ ^^

If i use #1 OK, but use # 2 error happend what's different?

#include <iostream>
/* -----------# 1------------
using std::endl;
using std::cout;
using std::eek:stream;
*/------------------------------

//-------------# 2--------------
using namespace std; //error!!! ' x' : cannot access private member
declared in class 'Point'
Are you sure? There doesn't look to be anything wrong with the code
(except the indentation!)

Which compiler, where exactly is the error?

<snip>
 
A

AngreGanon

tool is MS visual studio c++ 6.0

error message

Compiling...
test2.cpp
test2.cpp(20) : error C2248: 'x' : cannot access private member
declared in class 'Point'
test2.cpp(12) : see declaration of 'x'
test2.cpp(27) : error C2593: 'operator <<' is ambiguous
Error executing cl.exe.
 
I

Ian Collins

tool is MS visual studio c++ 6.0
That's probably your problem, upgrade to a decent compiler. Or dop all
the using nonsense and qualify the names.
error message

Compiling...
test2.cpp
test2.cpp(20) : error C2248: 'x' : cannot access private member
declared in class 'Point'
test2.cpp(12) : see declaration of 'x'
test2.cpp(27) : error C2593: 'operator <<' is ambiguous

Being an old compiler, its library probably has ostream outside of the
std namespace as well as inside.
 
D

Dave Rahardja

Hi, all~ ^^

If i use #1 OK, but use # 2 error happend what's different?

#include <iostream>
/* -----------# 1------------
using std::endl;
using std::cout;
using std::eek:stream;
*/------------------------------

//-------------# 2--------------
using namespace std; //error!!! ' x' : cannot access private member
declared in class 'Point'

class Point
{
private:
int x;
public:
Point(int _x=0):x(_x){}
friend ostream& operator<<(ostream& os, const Point& p);
};

ostream& operator<<(ostream& os, const Point& p)
{
os<<"["<<p.x<<"]"<<endl;
return os;
}

int main(void)
{
Point p;
cout << p;
return 0;
}

Comeau compiles your code with no errors. I suspect that the variable
_x may be colliding with something in the std namespace. There are two
suggestions I can make:

1. Don't use identifiers that start with underscores. They are reserved
for standard library use (17.4.3.1.2).
2. Don't make a habit of using the "using namespace" statement for the
reason you have observed above. Use the statement only if your module
is defining entities within the namespace itself.

-dr
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top