A confused C++ program

Amanda

新手上路
注册
2002-03-20
消息
43
荣誉分数
0
声望点数
0
I had a headache to compile the programm:

// englio.cpp
// overloaded << and >> operators
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
class Distance //English Distance class
{
private:
int feet;
float inches;
public:
Distance() : feet(0), inches(0.0) //constructor (no args)
{ }
//constructor (two args)
Distance(int ft, float in) : feet(ft), inches(in)
{ }
friend istream& operator >> (istream& s, Distance& d);
friend ostream& operator << (ostream& s, Distance& d);
};
//--------------------------------------------------------------
istream & operator >> (istream& s, Distance& d) //get Distance
{ //from user
cout << "\nEnter feet: "; s >> d.feet; //using
cout << "Enter inches: "; s >> d.inches; //overloaded
return s; //>> operator
}
//--------------------------------------------------------------
ostream& operator << (ostream& s, Distance& d) //display
{ //Distance
s << d.feet << "\'-" << d.inches << '\"'; //using
return s; //overloaded
} //<< operator
////////////////////////////////////////////////////////////////
int main()
{
Distance dist1, dist2; //define Distances
Distance dist3(11, 6.25); //define, initialize dist3

cout << "\nEnter two Distance values:";
cin >> dist1 >> dist2; //get values from user
//display distances
cout << "\ndist1 = " << dist1 << "\ndist2 = " << dist2;
cout << "\ndist3 = " << dist3 << endl;
return 0;
}


It produces the following errors:

------------------Configuration: Englio - Win32 Debug--------------------
Compiling...
Englio.cpp
H:\home\vc\test\lab04\Englio.cpp(23) : error C2248: 'feet' : cannot access private member declared in class 'Distance'
H:\home\vc\test\lab04\Englio.cpp(9) : see declaration of 'feet'
H:\home\vc\test\lab04\Englio.cpp(24) : error C2248: 'inches' : cannot access private member declared in class 'Distance'
H:\home\vc\test\lab04\Englio.cpp(10) : see declaration of 'inches'
H:\home\vc\test\lab04\Englio.cpp(30) : error C2248: 'feet' : cannot access private member declared in class 'Distance'
H:\home\vc\test\lab04\Englio.cpp(9) : see declaration of 'feet'
H:\home\vc\test\lab04\Englio.cpp(30) : error C2248: 'inches' : cannot access private member declared in class 'Distance'
H:\home\vc\test\lab04\Englio.cpp(10) : see declaration of 'inches'
H:\home\vc\test\lab04\Englio.cpp(40) : error C2593: 'operator >>' is ambiguous
H:\home\vc\test\lab04\Englio.cpp(42) : error C2593: 'operator <<' is ambiguous
H:\home\vc\test\lab04\Englio.cpp(43) : error C2593: 'operator <<' is ambiguous
Error executing cl.exe.

Englio.obj - 7 error(s), 0 warning(s)



I appreciate whom tell me if the result is different.
 
Your "#inclue" line is screwed up.
Try to change it to "#include<iostream.h>" and take out the "using namespace std" part. Or change it to "#include<iostream>" and keep the "using namespace std" part.

I have compiled and run the code. It worked OK except it didn't handle the float point correctly.
 
For some unknown reasons, I couldn't input &lt; and &gt; correctly. Is it because HTML/XML don't like this kind of characters? What I tried to type were "#include &lt;iostream.h&gt;" and
"#include &lt;iostream&gt;
using namespace std;"

After these little changes, the compilation and execution went smoothly.
 
Thanks.

In fact I have the "#include <iostream>" in the original program, don't know why it is lost
when I paste it.

Because it has "using namespace std;", the ".h" is not needed. It does not work in this
situation. But when I delete "using namespace std;" and plus the ".h" , it works.
I do not know why.
 
Good question!

I refered some technical stuff to answer your question:

" the c++ standardization committee decided to create new header names for the std-wrapped components. The algorithm they chose for generating the new header names is as trivial as the results it produces are jarring: the .h on the existing C++ headers was simply dropped. So <iostream.h> became <iostream>, <complex.h> became <complex>, etc. For a final twist, the old C++ headers were officially deprecated (i.e., listed as no longer supported), but the old C headers were not (to maintain C compatibility). In practice, compiler vendors have no incentive to disavow their customers' legacy software, so you can expect the old C++ headers to be supported for many years."

"Practically speaking, then, this is the C++ header situation:

1. Old C++ header names like <iostream.h> are likely to continue to be supported, even though they aren't in the official standard. The contents of such headers are not in namespace std.
2. New C++ header names like <iostream> contain the same basic functionality as the corresponding old headers, but the contents of the headers are in namespace std. (During standardization, the details of some of the library components were modified, so there isn't necessarily an exact match between the entities in an old C++ header and those in a new one.)
3. Standard C headers like <stdio.h> continue to be supported. The contents of such headers are not in std.
4. New C++ headers for the functionality in the C library have names like <cstdio>. They offer the same contents as the corresponding old C headers, but the contents are in std. "

So you now know why you can use both 1 and 2 are fine. But adding 'h' in 1 or deleting 'h' in 2 are all wrong.

(1) include <iostream>
using namespace std;

(2) include <iostream.h>
 
Sorry for making a typing error in the end part(BTW, above all symbol"<>" was parsed by URL link analyzation. Please select chinese input method to input it.

(1) #include <iostream>
using namespace std;

(2) #include <iostream.h>
 
弘毅老兄看来熟读《effective c++》,不过你引述的这段文字并没有回答Amanda的问题。这段代码我也试图编译过,非常奇怪的是用

#include <iostream.h>

一点问题没有,但是用

#include <iostream>
using namespace std

就出编译错误,目前还不太清楚为什么,只是直觉上感到肯定还是和namespace有关。
 
I think the problem is the library. The compiler does not automatically know where to look for the "std" namespace. Therefore, the libary file, "myStdLibrary.lib", which contains the "std" interface and implementation should be included in the link.

Haven't touched C++ for awhile, I chould be wrong for this, but this is the only reason I can think for now.
 
又试了一次,在unix环境下一次通过,使用命令如下:

gcc -c distance.cpp -o distance.o

连接也过了:

gcc -L/usr/lib -lstdc++ distance.o -o distance.bin

但是,在vc 6.0却死活也过不了,即便是编译成目标文件都不行。给微软写信吧,说不定是个bug。
 
最初由 大屁股 发布
又试了一次,在unix环境下一次通过,使用命令如下:

gcc -c distance.cpp -o distance.o

连接也过了:

gcc -L/usr/lib -lstdc++ distance.o -o distance.bin

但是,在vc 6.0却死活也过不了,即便是编译成目标文件都不行。给微软写信吧,说不定是个bug。
本人用 VC++ 6.0 with sp4, 在下属两种情况下都是一次过。
还是别忙给微软写信吧。
(1) #include <iostream>
using namespace std;

(2) #include <iostream.h>
 
我在WINDOWS2K+VC6.0的测试结果和九戒的结果是一致的, 所以我才引用了那段文字解释这种在VC6.0下的结果:

(1)通过VC6.0编译的原因: "New C++ header names(such as "iostream") like contain the same basic functionality as the corresponding old headers, but the contents of the headers are in namespace std."

(2)通过VC6.0编译的原因: "Old C++ header names(iostream.h) like are likely to continue to be supported, even though they aren't in the official standard. The contents of such headers are not in namespace std. "

其他的编译器我没有测试,所以不能下结论.请大HIP兄指正.
 
后退
顶部