s*****w 发帖数: 1527 | 1 1. for this code,
ostream& operator << (ostream& s, int cnt)
{
s << cnt;
return s;
}
i got
'operator <<' is ambiguous
why ? this is in VS 2005
if i change "int cnt" to "char c", it compiles.
2. also why ostream << overload has to return "ostream&" not "void" ?
i thought i'm passing in ostream&s anyway,
so when i use
ofstream f("myfile");
f<<55;
i don't see why the return "ostream &" got used.
many thanks ! |
|
g*******s 发帖数: 59 | 2 friend ostream & operator <<(ostream &output, const Complex &rhs)
{
output<<"("<
return output;
}
can compile and run well;
however if inside class define
friend ostream & operator <<(ostream &output, const Complex &rhs);
outside class
template
ostream & operator <<(ostream &output, const Complex &rhs)
{
output<<"("<
return output;
}
ERROR:
main.obj : error LNK2019: unresolved external symbol |
|
c**********e 发帖数: 2007 | 3 #include
using namespace std;
class Base {
protected:
virtual void output(std::ostream& os) { os << "Base\n"; }
};
class Derived : private Base {
friend std::ostream& operator<<(std::ostream&, Derived&);
};
std::ostream& operator<<(std::ostream& os, Derived& d) {
d.output(os);
}
int main() {
Base b; Derived d;
std::cout << b << d;
}
For which one of the following reasons is the code above INVALID?
a) Base::output is defined for Base, but operator<< is defined for Derived.
b) |
|
z****e 发帖数: 2024 | 4 接上面大侠的解释,我再说两句,ostream& 的返回是因为:
a. << 是 left associate 的,就是从左边开始一路 <<下去。每次<<结束就能update
一下这个被引用的ostream 对象,这样可以继续<<后面的。就是 concatenate.
b. 只要是返回一个引用,你就要想到,这是因为该函数的返回值可以作为left value
使用。 |
|
x******a 发帖数: 6336 | 5 I found if I make the second parameter const, i.e.std::ostream& operator<<(
std::ostream& os, const str& s), it worked now. what happened?
std::ostream& operator<<(std::ostream& os, str& s){
//obliterate existing value(s)
for (str::size_type i=0; i!=s.size(); ++i)
os<
return os;
}
class str{
public:
typedef std::vector::size_type size_type;
//constructors
str();
str(size_type n,char c);
str(const char* cp);
template str(In b, ... 阅读全帖 |
|
s*****n 发帖数: 231 | 6 cout is an object of class ostream that represents the standard output
stream.
and ostream is the base class of ofstream, which you use to output to a file.
So in A. void print(std::ostream &os);
if you pass a reference to ostream object in the print function, it's OK for
both type.
But in B.void print(std::ofstream is);
Not only you cannot output to the standard output, but also it's passed by
value, that means you make a copy of the ostream object you passed in, and
do output in that copy, whi... 阅读全帖 |
|
j*****k 发帖数: 1198 | 7 template
ostream& operator<<(ostream& os, const A& rhs)
{
return rhs.out(os);
};
error: passing ‘const A’ as ‘this’ argument of ‘std::ostream& A<
T>::out(std::ostream&) [with T = float]’ discards qualifiers
这儿T是float. A里面有funcion out(ostream& os)用来显示内容
A a(2);
cout< |
|
t****t 发帖数: 6806 | 8 OK, solved. let me explain this, this is complex.
you called copy(....begin(), ....end(), ostream_iterator<...>(...)), so
ostream_iterator will do the << for you. naturally, ostream_iterator is
within namespace std, so operator<< is called in namespace std.
Now for overload resolution. there are a lot of operator<< within namespace
std; so these are considered. your parameters to operator<< are std::basic_
ostream<...> and std::pair<...>, they are both in namespace std. Therefore
no other namesp... 阅读全帖 |
|
l*******y 发帖数: 1498 | 9 class A
{
private:
string name;
public:
....
friend ostream& operator<< (const ostream& out, const A& a);
};
ostream& operator<< (const ostream& out, const A& a)
{
out << a.name;
return out;
} |
|
l*******y 发帖数: 1498 | 10 你看看是什么错误再修改一下不就可以了
#include
#include
using namespace std;
class A
{
private:
string name;
public:
A(string n):name(n){}
friend ostream& operator<< (ostream &out, const A &a);
};
ostream& operator<< (ostream &out, const A &a)
{
out << a.name;
return out;
}
int main()
{
A aa("test");
cout<
return 0;
} |
|
h*****g 发帖数: 944 | 11 谢谢大家帮我看看,我实在不知道为啥错了
#include
using namespace std;
template< class T>class TreeNode{
public:
T data;
TreeNode(T v);
TreeNode *left;
TreeNode *right;
friend ostream &operator<<(ostream &stream, TreeNode &o);
};
template TreeNode ::TreeNode(T v)
:data(v), left(NULL), right(NULL)
{
}
template ostream &operator<<(ostream &stream, TreeNode &o){
stream<data<<" ";
}
int main(){
TreeNode * head = n... 阅读全帖 |
|
c****o 发帖数: 1280 | 12 the operator is also a template operator, you need to first claim it as a
template operator, like
template
class TreeNode;
template
ostream & operator <<(ostream & stream, TreeNode & O)
template
class TreeNode{
...
friend ostream &operator<< <>(ostream &stream, TreeNode &o);
..
};
template
then implement the operator |
|
D*****r 发帖数: 6791 | 13 http://www.ariel.com.au/jokes/The_Evolution_of_a_Programmer.html
High School/Jr.High
10 PRINT "HELLO WORLD"
20 END
First year in College
program Hello(input, output)
begin
writeln('Hello World')
end.
Senior year in College
(defun hello
(print
(cons 'Hello (list 'World))))
New professional
#include
void main(void)
{
char *message[] = {"Hello ", "World"};
int i;
for(i = 0; i < 2; ++i)
printf("%s", message[i]);
printf("\n");
}
... 阅读全帖 |
|
t******m 发帖数: 255 | 14 #include
templateclass Rational;//forward deceleration
template std::ostream& operator<< (std::ostream& , const
Rational& ); //为什么这里用 不行。而在class里要用
templateclass Rational
{
public:
Rational(const T & top,const T & bottom):Numerator(top),Denominator(
bottom){}
const T& Num()const{return Numerator;}
const T& Dem()const{return Denominator;}
virtual ~Rational(){}
friend std::ostream& operator<< (std::ostream& , const Ratio |
|
N***m 发帖数: 4460 | 15 effective c++上面的讲proxy class,给的例子只有轮廓,但没有具体实现。
我就练习一下。问题是
ostream& operator << (ostream& os, const Array2D & a)必须要const_cast,
不然编译不通过。我尝试把Array1D operator[](int pos)声明为const,也不行。
我哪个地方搞错了?还有typeid(T)返回的是i和3Foo,不是integer和Foo,
这个是不是和编译器有关?
/*
use of proxy class
*/
#include
#include
using namespace std;
// class Foo is some type for test Array2D later;
class Foo {
int val;
public:
Foo(){}
Foo(int i):val(i){}
friend ostream & operator<<(ostream& os, const Foo & b);
} |
|
s*****k 发帖数: 604 | 16 困扰多时的MATLAB crash问题
谁有matlab的帮我运行一下下面的程序。看看是不是和我一样的情况。
平时用matlab比较多。最近在matlab帮助文挡里看了一点
如何在matlab里使用java类,在好奇心驱使下用matlab
写了一个简单的web服务器,原理就是调用java.net.serversocket。
其实我java基本不会,但是稍微看了看文档还是大概能明白
怎么使用ServerSocket类的。
我写了一个简单web服务器,运行正常,可以serve静态网页。
然后我又想改进一下以便这个服务器可以用matlab语言做脚本
产生动态网页。
然后我就修改代码,并且没有保存旧的代码,改了一会发现
一运行程序,matlab就会crash。连debug都没法做,你只要在
源文件里面设置断点就能导致matlab crash,然后matlab必须
关掉重新启动。我昨晚找了一晚上bug都不知道哪里有问题。
高手帮我看看下面的程序哪里会造成这个问题。
说明一下,我只要把循环改成 for k=1:2 和 end 注释掉就没问题了。
但是这样只能serve一个 浏览器的request了... 阅读全帖 |
|
X***X 发帖数: 302 | 17 我做overloading 时遇到的问题,头都大了。
#include
using namespace std;
using std::ostream;
using std::istream;
class Array{
friend ostream &operator<<( ostream &, const Array &);
friend istream &operator>>( istream &, Array &);
public:
Array(int=10);
Array(const Array &);
Array &operator=(const Array &);
Array &operator+=(const Array &);
int &operator[](int);
private:
int length;
int *ptr;
};
然后一个cpp的文件,定义overloading:
#include |
|
g*******s 发帖数: 59 | 18 Three files:
complex.h
#ifndef COMPLEX_H
#define COMPLEX_H
#include
using namespace std;
template
class Complex{
friend ostream & operator << (ostream &output, const Complex &rhs);
private:
T real;
T imaginary;
public:
explicit Complex(T x, T y):real(x),imaginary(y){}
~Complex(){}
Complex & operator +(const Complex &rhs) const;
.
.
.
};
#endif
complex.cpp
#include "complex.h"
using namespace std;
template
ostream & operator |
|
k**f 发帖数: 372 | 19
No, because when you do so, the left hand side operand is the object. In the
case of operator<<, the lhs operand is the ostream.
On the other hand, it can be implemented as a non-friend free standing
function, if the output operation do not need to use the private or
protected data members or functions.
Example:
class foo {
private:
int m;
public:
foo(int n)
int value() const { return m; }
};
ostream& operator<<(ostream& os, const foo& f)
{
return os << f.value();
} |
|
z****e 发帖数: 2024 | 20 为什么不可以呢?
注意 class B's initialization list: "_obja(i,j)".
#include
using std::ostream;
class A{
public:
A(int a, int b):_a(a),_b(b){}
int _a;
int _b;
};
class B{
public:
B(int i, int j):_obja(i,j){}//pay attention here
private:
A _obja;
friend ostream& operator<<(ostream& os,const B& b){
os<
return os;
}
};
main(){
using namespace std;
B objb(1,2);
cout<
} |
|
d****i 发帖数: 4809 | 21 std::cout<< s1+s2 <
std::ostream::operator<<(std::cout, s1) + s2
这里你并没有定义ostream::operator+(std::ostream& os, str& s), 所以第二个"+"
号没法通过。试试把(s1+s2)放在括号里? |
|
x******a 发帖数: 6336 | 22 I got thousands problems on the following piece of code "dumpfile.h" when I
compile under cygwin. it is ok under visual stduio... can anyone help?
Thanks!
#include
#include
#include //ostream_iterator
#include //cerr
#include //std::copy
template
void dump_to_file(const char* filename, const std::vector& v_d){
std::ofstream ofs(filename);
if(!ofs){
std::cerr<<"Unable to open the file to write!n";
return ;... 阅读全帖 |
|
s********l 发帖数: 998 | 23 不知道你怎么定义multi inheritance
反正 是个多层的inheritance
比如 iostream 是继承了ostream & istream
ostream & istream是 virtual inheritance ios
inheritan |
|
l********n 发帖数: 54 | 24 有一个类
class A
{
};
要使得下面的code能够编译
int main(void)
{
A a;
cout<
return 1;
}
并且cout<
问需要在这个类里添加什么functions,如何实现。
============
应该是要对<<进行operator overloading
ostream& operator<< (const ostream& out, const A& a);
不过不知道如何实现。
PS: 建议用用这个工具,这样大家可以讨论code起来比较方便。
http://www.ideone.com/kNSiq |
|
l********n 发帖数: 54 | 25 这个code没有问题,可以正确运行。
不过我还有一个问题,就是为什么要用友元函数,而不能作为成员函数。就像普通
operator overloading.我试过了,有
compiler error.不过不是很明白为什么。
class A
{
public:
ostream& operator<< (ostream &out, const A &a)
{
out << a.name;
return out;
}
} |
|
g**u 发帖数: 583 | 26 用stl的priority_queue写了下multi-way merge, 请大家拍砖。
其中: myPair的第一个就是要存储的值, 第二个表示ownership.
/*
*multiple way merge using the priority_heap
*/
#include "vector"
#include "queue"
class myPair{
friend std::ostream & operator<<(std::ostream & out, const myPair &p)
{
out<<" ( "<
return out;
}
public:
myPair(int x, int y):_first(x),_second(y)
{
}
bool operator >(const myPair & p)const
{
return this->_fir... 阅读全帖 |
|
s*****n 发帖数: 231 | 27 template ostream &operator<<(ostream &stream, TreeNode &o){
stream<data<<" ";
//add your return value here!
return stream;
} |
|
h*****g 发帖数: 944 | 28 谢谢啊
好像我pass到那个override function的变量是个pointer, 所以要o->data吧
不然的话是不是要写成一下的样子?
template ostream &operator<<(ostream &stream, TreeNode *&o){ |
|
p*i 发帖数: 411 | 29 #include
#include
#include
#include
using namespace std;
inline void swap(int &a, int &b) {
int t = a; a = b; b = t;
}
int partition(vector& work, vector& indices, int p, int q) {
// use work[q] to partition the array
const int x = work[q];
int i = p-1;
for (int j = p; j < q; j++) {
if (work[j] < x) {
i++;
swap(work[i], work[j]);
swap(indices[i], indices[j]);
}
}
i++;... 阅读全帖 |
|
j********x 发帖数: 2330 | 30 写了一个小时,没考虑特别的corner case,O(num of intervals of "except"),用了
上面提到的binary search:
#include
#include
#include
#include
#include
struct compareable_interval {
int start;
int end;
int real_start;
int real_end;
compareable_interval(int lhs, int rhs) : start(lhs), end(rhs), real_
start(lhs), real_end(rhs) {
}
compareable_interval(int l, int r, int rl, int rr) : start(l), end(r),
real_start(rl), real_end(rr) {
}
b... 阅读全帖 |
|
j********x 发帖数: 2330 | 31 写了一个小时,没考虑特别的corner case,O(num of intervals of "except"),用了
上面提到的binary search:
#include
#include
#include
#include
#include
struct compareable_interval {
int start;
int end;
int real_start;
int real_end;
compareable_interval(int lhs, int rhs) : start(lhs), end(rhs), real_
start(lhs), real_end(rhs) {
}
compareable_interval(int l, int r, int rl, int rr) : start(l), end(r),
real_start(rl), real_end(rr) {
}
b... 阅读全帖 |
|
r*********n 发帖数: 4553 | 32 this is why ostream& is returned instead of void
okay, so if << were to be defined as a member, it would be a member of
ostream class, making it impossible to overload |
|
I**A 发帖数: 96 | 33 I got a piece of Vbscript to download files from Internet. It works fine for
small files but failed for large file( greater than 3M?). I guess the
buffer size is not set large enought. How can I set buffer size in Vbscript?
Thanks a lot.
Here is the section of the code:
dim sWebAddress : sWebAddress = Wscript.Arguments.Item(0)
dim oxMLHTTP: set oxMLHTTP=CreateObject("MSXML2.ServerXMLHTTP")
dim oStream: Set oStream = CreateObject("ADODB.Stream")
With oXMLHTTP
.Open "GET", sWebAddress, true
|
|
q*****g 发帖数: 72 | 34 the friend keyword is need when u want to overload operator<<
if the operator << is a member function, to output the class, we need
to write this:
Widget << cout; // Widget is a object of someclass
This is against common practise.
if use friend function, the function declaratoin will like this:
friend ostream& operator<< (ostream&, Widget&);
so we can write this:
cout << widget;
which is much clearer. |
|
j*****k 发帖数: 1198 | 35 ostream& out(ostream&);
没设const, 一定要设么 |
|
d****2 发帖数: 6250 | 36
of course float can be template typename.
add const to member function "out" definition:
template
...inside class A...{
ostream &out(ostream & ) const {...}}
|
|
j*********g 发帖数: 3826 | 37 现在我想用iostream实现一个ostream类,我用的是.net 2005,不会玩也不准备玩C#。
这个ostream需要输出到文件,到console window,以后可能还要有输出到GUI的功能。
2个问题,
1。MFC GUI程序,现在的iostream库里面好像没有stdiostream这个类了,即使VC6有,
我试过AllocConsole,然后没有办法把这个handle和stream挂上。
2。因为我要有若干个输出目标,所以似乎我必须重载这个stream的所有输出函数。好
像很麻烦的样子。
我没怎么用过C++,很土,这里虚心请教,有什么好办法实现这个东西吗? |
|
n**d 发帖数: 9764 | 38 So far I have seen to override operator<< by friend only, like this.
friend ostream&
operator<<(ostream& os, const Number& x) {...}
How to implement this as a member function, instead of friend? Possible? |
|
E*****7 发帖数: 128 | 39 #include
using namespace std;
class Top {
protected:
int x;
public:
Top(int n) { x = n; }
virtual ~Top() {}
friend ostream&
operator<<(ostream& os, const Top& t) {
return os << t.x;
}
};
class Left : virtual public Top {
protected:
int y;
public:
Left(int m, int n) : Top(m) { y = n; }
};
class Right : virtual public Top {
protected:
int z;
public:
Right(int m, int n) : Top(m) { z = n; }
};
class Bottom : public Left, public Right {
int w;
public:
Bottom(int |
|
l***e 发帖数: 480 | 40 class aaa {
public:
string str;
int i;
ostream & operator<< (ostream & stream) {
stream << str << " " <
return stream;
}
}
OK? |
|
l***e 发帖数: 480 | 41 通过了,执行正确。
如何定义在类外?
class aaa {
public:
string str;
int i;
}
edge::ostream & operator<< (ostream & stream) {
stream << str << " " <
return stream;
} |
|
S*********g 发帖数: 5298 | 42 As someone else have said, you usually want to define
it as friend non-member function since you will probably
need to access private/protected informations of class aaa in this function.
class aaa{
private:
string str;
int i;
friend ostream& operator<<(ostream& os, const aaa& a)
{
return os << a.str << " "<< a.i << std::endl;
}
}; |
|
g*******s 发帖数: 59 | 43 friend ostream& operator<<(ostream &, const Counter &);
为什么要加个const before class name? |
|
z****e 发帖数: 2024 | 44 那个代码是我自己测试写的,
现在我改成这个:
class B{
public:
B(int i, int j, A a):_obja(a){}//pay attention here
private:
A _obja;
friend ostream& operator<<(ostream& os,const B& b){
os<
return os;
}
};
也行。
是不是说 initialization list 的参数可以是成员的构造函数形式,也可以是拷贝构
造函数?就是两种构造函数都能用在初始化列表里面? |
|
r*********r 发帖数: 3195 | 45 1. the system has this operator defined already.
2. f << 55 << 10; |
|
s*****w 发帖数: 1527 | 46
but why "char" worked ?
good point, thx ! |
|
r*********r 发帖数: 3195 | 47 because << "char" is a stupid idea. so the system doesn't have this.
the purpose of << is to translate every data type except "char" to
a stream of "char"s. why would u want to translate char to itself? |
|
|
z****e 发帖数: 2024 | 49 一个矩阵类,用vector< vector >.结构
问题是能否把下面那个while改成for_each?
#include
#include
#include
#include
using namespace std;
class Matrix{
friend ostream& operator<<(ostream & os, const Matrix & Mat){
vector< vector >::const_iterator iter=Mat._m.begin();
while(iter!=Mat._m.end())//此处能否不用循环?
Mat.printrow( *(iter++), os);//比如改成for_each?
return os;
}
public:
Matrix(){}
Matrix(int row, int column):
_m( vector< vector > (row, vecto |
|
z****e 发帖数: 2024 | 50 template std::ostream& operator<< (std::ostream& , const
Rational& );
把operator<<后边的去掉。
你加上,是一个specialization的形式, 但是无从specialize 起,本身就错误的语法。
另外,你根本不需要friend呀,都是通过公有interface干的事情。
如果class里边没有,是一个普通函数,你没定义过,编译器找不到。
或者你根本用不着模板,就直接把friend定义class里边,也不用,也不用forward delcare,这样,每次,class模板生成一次,都新生成一个overload friend,的普通函数。
明白否? |
|