ostream& tab (ostream &o)
{
return o << '\t';
}
Run Code Online (Sandbox Code Playgroud)
我想把这个声明放在iostream库中.我可以这样做吗?
class two;
class one
{
int a;
public:
one()
{
a = 8;
}
friend two;
};
class two
{
public:
two() { }
two(one i)
{
cout << i.a;
}
};
int main()
{
one o;
two t(o);
getch();
}
Run Code Online (Sandbox Code Playgroud)
我从Dev-C++得到这个错误:
a class-key must be used when declaring a friend
Run Code Online (Sandbox Code Playgroud)
但是使用Microsoft Visual C++编译器编译时运行正常.
// By using structure :
struct complex {
float real;
float imag;
};
complex operator+(complex, complex);
main() {
complex t1, t2, t3;
t3 = t1 + t2;
}
complex operator+(complex w, complex z) {
statement 1;
statement 2;
}
// By using class :
class complex {
int real;
int imag;
public:
complex operator+(complex c) {
statement 1;
statement 2;
}
main() {
complex t1, t2, t3;
t3 = t1 + t2;
}
Run Code Online (Sandbox Code Playgroud)
在使用结构时,重载函数可以接受两个参数,而在使用类时,重载函数只接受一个参数,当重载操作符函数在两种情况下都是成员函数时,即在struct和class中.为什么会这样?
#include<iostream>
using namespace std;
class test
{
int a, b;
public:
test() {
a=4; b=5;
}
test(int i,int j=0) {
a=i; b=j;
}
test operator +(test c) {
test temp;
temp.a=a+c.a;
temp.b=b+c.b;
return temp;
}
void print() {
cout << a << b;
}
};
int main() {
test t1, t2(2,3), t3;
t3 = t1+2;
t3.print();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译器如何能接受像一份声明t3=t1+2;,其中2没有一个对象?
我试图MessageBox使用这段代码做一个简单的事情:
#include <windows.h>
int main() {
MessageBox(NULL, "Hello", "Message Box", MB_OKCANCEL);
}
Run Code Online (Sandbox Code Playgroud)
但是在使用MinGW工具链在Dev-C++ IDE中构建它之后,我会在后面弹出一个控制台窗口MessageBox.
有没有办法摆脱这个控制台窗口?
import java.io.*;
class demo
{
public static void main(String args[])
{
PrintWriter pw=new PrintWriter(System.out);
pw.println("java");
//pw.print("java");
}
}
Run Code Online (Sandbox Code Playgroud)
//输出正在java使用,pw.println但输出为空,pw.print即使用时控制台上没有任何内容打印print.
c++ ×5
visual-c++ ×2
class ×1
console ×1
dev-c++ ×1
io ×1
java ×1
manipulators ×1
mingw ×1
oop ×1
ostream ×1
printwriter ×1
structure ×1
windows ×1