Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

請問高手們 Visual C++ 的問題 謝謝!!!

0 views
Skip to first unread message

黃金體驗

unread,
Jan 17, 2000, 3:00:00 AM1/17/00
to
各位高手們 請問一下,到底是為什麼??
將程式附於下,多謝各位撥冗回答 謝謝!!

我在Visual C++ 中,寫了一個簡單的class
想要overload operator << 和 >> 兩個operator給iostream用

(1)如果我#include <iostream> //其 namespace 在std 中
出現錯誤訊息如下,但同樣的程式在BCB中卻又可以執行??
1)為什麼operator>>() 是friend function 卻無法存取private member?
2)為什麼明明只有一個引數為MyClass的operator>>() ,卻是ambiguous?
3)為什麼同樣的程式在BCB又可以執行
4)因為我寫的程式,有時候包含的class是屬於在std裡的,也會包含<iostream>
,所以若是用<iostream.h>,會產生重複定義的問題,所以最好是能夠只使用
<iostream>,請問該如何解決呢?謝謝
--------------------Configuration: test002 - Win32 Debug--------------------
Compiling...
test002.cpp
d:\cpra\vc\test002\myclass.h(25) : error C2248: 'i'
: cannot access private member declared in class 'MyClass'
d:\cpra\vc\test002\myclass.h(18) : see declaration of 'i'
.
.
D:\cpra\Vc\test002\test002.cpp(10) : error C2593: 'operator <<' is ambiguous
Error executing cl.exe.

test002.exe - 5 error(s), 0 warning(s)

--------------------Configuration: test002 - Win32 Debug-------------------


(2)如果我#include <iostream.h> //其 namespace 在全域中
則可以執行


//test002.cpp

#include "MyClass.h"
int main(int argc, char* argv[])
{
MyClass a(1,2),b(3),c(a);
cout<<a<<endl<<b<<endl<<c<<endl;
cin.get();

return 0;
}

// MyClass.h
#ifndef __MyClass
#define __MyClass

#include <iostream> // 若改成#include <iostream.h> 則可以執行!!

namespace std{};using namespace std;

class MyClass{
public:
MyClass(int i=0,int j=0):i(i),j(j){};
MyClass(const MyClass& A):i(A.i),j(A.j){};
int vi(){return i;}
int vj(){return j;}
friend void test2(MyClass &o);
friend ostream& operator<<(ostream& ov,MyClass &o);
friend istream& operator>>(istream& ov,MyClass &o);
private:
int i;
int j;

};
ostream& operator<<(ostream& ov,MyClass &o) {
ov<<"<"<<o.i<<","<<o.j<<">\t";
return ov;
}

istream& operator>>(istream& ov,MyClass &o) {
ov>>o.i>>o.j;
return ov;
}
#endif

--
--
※ Origin: 楓橋驛站<bbs.cs.nthu.edu.tw> ◆ From: chi14.ieo.nctu.edu.tw

jon

unread,
Jan 17, 2000, 3:00:00 AM1/17/00
to

這是VC 6.0的一個bug...sp3已修正...
我試過了沒問題...
詳見knowledge base
Q192539 - FIX: 'using namespace std' Before Friend Operator Fails Compile
另外提醒你一下...iostream.h和<iostream>裡定義的class是不一樣的...
所以behaviour不見得會相同...
--
[1;32m※ Origin: [33m交大資工鳳凰城資訊站 [37m<bbs.csie.nctu.edu.tw> [m
[1;31m◆ From: [36mt199-82.dialup.seed.net.tw [m

黃金體驗

unread,
Jan 17, 2000, 3:00:00 AM1/17/00
to
※ 引述《freak...@bbs.csie.nctu.edu.tw (jon)》之銘言:

> 這是VC 6.0的一個bug...sp3已修正...
> 我試過了沒問題...
> 詳見knowledge base
> Q192539 - FIX: 'using namespace std' Before Friend Operator Fails Compile
> 另外提醒你一下...iostream.h和<iostream>裡定義的class是不一樣的...
> 所以behaviour不見得會相同...

不過請問knowledge base 是什麼該在哪兒查?

真是多謝你了
--
※ Origin: 楓橋驛站<bbs.cs.nthu.edu.tw> ◆ From: chi13.ieo.nctu.edu.tw

jon

unread,
Jan 17, 2000, 3:00:00 AM1/17/00
to
※ 引述《Caesa...@bbs.cs.nthu.edu.tw (黃金體驗)》之銘言:

> ※ 引述《freak...@bbs.csie.nctu.edu.tw (jon)》之銘言:
> > 這是VC 6.0的一個bug...sp3已修正...
> > 我試過了沒問題...
> > 詳見knowledge base
> > Q192539 - FIX: 'using namespace std' Before Friend Operator Fails Compile
> > 另外提醒你一下...iostream.h和<iostream>裡定義的class是不一樣的...
> > 所以behaviour不見得會相同...
> 不過請問knowledge base 是什麼該在哪兒查?
> 真是多謝你了

knowledge base是微軟technical support的一部份....
裡面的內容包括了MS產品技術資訊、修正清單、說明文件錯誤、faq等...
msdn library裡面有..不然上線查最完整...
http://support.microsoft.com/support/


--
[1;32m※ Origin: [33m交大資工鳳凰城資訊站 [37m<bbs.csie.nctu.edu.tw> [m

[1;31m◆ From: [36mt197-41.dialup.seed.net.tw [m

0 new messages