小编shu*_*jan的帖子

继承和组合有什么区别?

正如标题所说,两者的含义都让我难以理解。

oop design-patterns

6
推荐指数
1
解决办法
1万
查看次数

如何在c ++中将自己的函数声明放在iostream库中?

ostream& tab (ostream &o)
{
    return o << '\t';
}
Run Code Online (Sandbox Code Playgroud)

我想把这个声明放在iostream库中.我可以这样做吗?

c++ ostream manipulators

5
推荐指数
1
解决办法
160
查看次数

如何解决"声明朋友时必须使用的类"错误?

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++编译器编译时运行正常.

c++ dev-c++ visual-c++ friend-function

4
推荐指数
2
解决办法
2812
查看次数

结构与类

// 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中.为什么会这样?

c++ structure class

4
推荐指数
1
解决办法
213
查看次数

为什么会发生此转换?

#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没有一个对象?

c++ operator-overloading visual-c++

4
推荐指数
1
解决办法
96
查看次数

如何摆脱控制台窗口

我试图MessageBox使用这段代码做一个简单的事情:

#include <windows.h>

int main() {
  MessageBox(NULL, "Hello", "Message Box", MB_OKCANCEL);
}
Run Code Online (Sandbox Code Playgroud)

但是在使用MinGW工具链在Dev-C++ IDE中构建它之后,我会在后面弹出一个控制台窗口MessageBox.

有没有办法摆脱这个控制台窗口?

c++ windows console mingw

2
推荐指数
1
解决办法
2081
查看次数

java中的PrintWriter给出了意想不到的行为

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.

java io printwriter

2
推荐指数
2
解决办法
373
查看次数