RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / user-406526

VanechikSpace's questions

Martin Hope
VanechikSpace
Asked: 2022-08-23 17:12:11 +0000 UTC

通用方法和上限

  • 2

分为三类:

class A{
}

class B{
}

class Gen<T, V extends T>{
}

以下程序将无法编译:

class Main{
    public static void main(String[] args){
        Gen<A, B> g;
    }
}

因为它V必须与它相同T或它的子类。

现在有这段代码:

class Main{
    public static <T, V extends T> void qq(T x, V y){
    }

    public static void main(String[] args){
        qq(new A(), new B());
    }
}

我希望它不会以相同的方式编译,因为它B不继承自A,但它仍然工作得很好。为什么?

java generics
  • 1 个回答
  • 24 Views
Martin Hope
VanechikSpace
Asked: 2022-08-08 21:44:11 +0000 UTC

Thread 类是否使用多个内核?

  • 3

我刚开始学习多线程编程。我了解到,如果处理器有一个内核,那么线程不会同时执行,而只会在彼此之间周期性地切换。反过来,多核处理器提供两个不同线程的实际同时执行。

问题:该类是否使用Thread()多个核心,还是需要做其他事情?

java многопоточность
  • 1 个回答
  • 62 Views
Martin Hope
VanechikSpace
Asked: 2022-07-26 22:43:11 +0000 UTC

getSuppressed() 方法是如何工作的?

  • 0

我了解到,如果try с ресурсами在块中抛出异常,并且在块finally中抛出另一个异常,那么原始异常不会丢失,而是被抑制并且可以使用方法获得getSuppressed()。为了测试这一点,我编写了以下代码:

import java.io.*;

class test implements AutoCloseable{
        public void close(){
                throw new RuntimeException();
        }
}

class Main{
        static void qq(){
                try(test q = new test()){
                }
                finally{
                        throw new Error();
                }
        }

        public static void main(String[] args){
                try{
                        qq();
                }
                catch(Throwable a){
                        System.out.println(a.getSuppressed().length);
                }
        }
}

我本来希望得到 1,但令人惊讶的是,我得到了 0。

1、为什么这里显示的是0而不是1?

2. 你能举个例子,getSuppressed().length 将输出大于 0。

java
  • 1 个回答
  • 38 Views
Martin Hope
VanechikSpace
Asked: 2022-09-13 23:25:48 +0000 UTC

关闭程序后如何显示退出代码?

  • 0

我使用 mingw g++ 在 windows cmd 中编译程序。每次关闭程序后,我都想查看退出代码。比如我在使用mingw的ide clion时,程序成功完成后,在控制台看到如下:

在此处输入图像描述

例如,如果程序没有成功结束,那么我会看到不同的画面:

在此处输入图像描述

也就是说,我看到了程序完成执行的代码。

如果我用 mingw 编译相同的程序,但只在 cmd 中。无论结果是否成功,我都不会看到程序结束的代码:

在此处输入图像描述

是否有可能让我在 cmd 中看到这个退出代码?

gcc
  • 1 个回答
  • 10 Views
Martin Hope
VanechikSpace
Asked: 2022-06-21 21:27:50 +0000 UTC

文件输出到特定位置

  • 0

我有文件qwer.h。它写在里面hello, misha。我想在dear后面加一个词hello。

#include <iostream>
#include <fstream>

int main()
{
        std::ofstream ff1{"qwer.h", std::ios::ate};

        ff1.seekp(5, std::ios::beg);

        ff1 << "dear" << std::endl;;

        return EXIT_SUCCESS;
}

结果,在文件中qwer.h我得到以下内容:^@^@^@^@^@dear. 我尝试更改std::ofstream ff1{"qwer.h", std::ios::ate};为std::ofstream ff1{"qwer.h", std::ios::ate | std::ios::app};,但随后这些单词被简单地添加到文件末尾。

c++
  • 1 个回答
  • 10 Views
Martin Hope
VanechikSpace
Asked: 2022-05-08 20:41:48 +0000 UTC

显式类模板特化和头文件

  • 0

共有三个文件:

主.cpp:

#include "test.h"

int main()
{
        A<int> ai;
        ai.print();

        A<double> ad; 
        ad.print();

        A<char> ac;
        ac.print();

        return 0;
}

测试.cpp:

#include <iostream>
#include "test.h"

template<typename Type>
void A<Type>::print()
{
        std::cout << "some" << std::endl;
}

//template<>
//class A<int>
//{
//public:
//      void print()
//      {
//              std::cout << "int" << std::endl;
//      }
//};

template class A<int>;
template class A<double>;
template class A<char>;

测试.h:

#ifndef TEST_H
#define TEST_H

template<typename Type>
class A
{
public:
        void print();
};

#endif

显示:

some
some
some

我想对类模板进行明确的专业化A,为此我将取消注释注释行。结果:

/usr/bin/ld: /tmp/cc5Vvv83.o: in function `main':
/home/ivan/first/main.cpp:6: undefined reference to `A<int>::print()'
collect2: error: ld returned 1 exit status

如果我复制这个定义并将其移动到标题中,那么程序将运行并给出它需要的内容:

int
some
some

但我不想把方法定义A<int>::print()放在标题中,所以我test.h把它留在:

template<>
class A<int>
{
public:
      void print();
};

test.cpp我把:

template<>
void A<int>::print()
{
        std::cout << "int" << std::endl;
}

结果:

test.cpp:11:6: error: template-id ‘print<>’ for ‘void A<int>::print()’ does not match any template declaration
   11 | void A<int>::print()
      |      ^~~~~~
In file included from test.cpp:2:
test.h:15:14: note: candidate is: ‘void A<int>::print()’
   15 |         void print();
      |              ^~~~~

问题: 1)在这种情况下,如何将类的定义保留在A<int>中test.h,将方法的定义保留A<int>::print()在and 中test.cpp,以便程序可以工作?2)处理这种情况的正确方法是什么?也许我们应该把定义留在头文件中而不用打扰(尽管据我所知,这样我们会增加编译时间)?

c++
  • 1 个回答
  • 10 Views
Martin Hope
VanechikSpace
Asked: 2022-04-30 16:20:49 +0000 UTC

c++中受保护的构造函数

  • 1

我知道这样做很可能毫无意义,但我对结果的原因很感兴趣。

为什么这个代码:

#include <iostream>

class A
{
protected:

        A()
        {
        }

};

class B : public A
{
public:
        void f()
        {
                A a;
        }       
};

int main()
{
        return 0;
}

给出错误消息:

test.cpp:18:5: error: ‘A::A()’ is protected within this context
   18 |   A a;
      |     ^
test.cpp:7:2: note: declared protected here
    7 |  A()
      |  ^

毕竟,B 类可以使用 A 类的 Protected 成员。

c++
  • 1 个回答
  • 10 Views
Martin Hope
VanechikSpace
Asked: 2022-04-24 23:00:04 +0000 UTC

协变返回类型

  • 4

为什么这个代码:

#include <iostream>
 
class Parent
{
public:
    virtual Parent* getThis() { std::cout << "called Parent::getThis()\n"; return this; }
    void printType() { std::cout << "returned a Parent\n"; }
};
 
class Child : public Parent
{
public:
    virtual Child* getThis() { std::cout << "called Child::getThis()\n";  return this; }
    void printType() { std::cout << "returned a Child\n"; }
};
 
int main()
{
    Child ch;
    Parent *p = &ch;
    ch.getThis()->printType(); 
    p->getThis()->printType();
}

它会产生以下结果吗?

called Child::getThis()
returned a Child
called Child::getThis()
returned a Parent

实际上,在最后一行应该显示returned a Child它,因为它p->getThis()返回一个指向Child

c++
  • 3 个回答
  • 10 Views
Martin Hope
VanechikSpace
Asked: 2022-04-10 18:11:32 +0000 UTC

直接类初始化

  • 0

为什么下面的程序输出而不是helloand ?0/11

#include <cassert>
#include <iostream>
 
class Drob
{
private:
    int m_numerator;
    int m_denominator;
 
public:
    Drob(int numerator=0, int denominator=1) :
        m_numerator(numerator), m_denominator(denominator)
    {
        std::cout << "hello" << std::endl;
        assert(denominator != 0);
    }
 
    Drob(const Drob &drob) :
        m_numerator(drob.m_numerator), m_denominator(drob.m_denominator)
    {
        std::cout << "Copy constructor worked here!\n"; 
    }
 
    friend std::ostream& operator<<(std::ostream& out, const Drob &d1);
};
 
std::ostream& operator<<(std::ostream& out, const Drob &d1)
{
    out << d1.m_numerator << "/" << d1.m_denominator;
    return out;
}
 
int main()
{
    Drob sixSeven (Drob());
    std::cout << sixSeven << std::endl;
    return 0;
}

如果Drob sixSeven (Drob());用Drob sixSeven {Drob()};或 用Drob sixSeven = Drob();then 替换,结果会是helloand 0/1,但是直接初始化的情况下,结果就不同了。

c++
  • 1 个回答
  • 10 Views
Martin Hope
VanechikSpace
Asked: 2022-03-19 22:48:29 +0000 UTC

通过引用将 lambda 传递给函数

  • 0

有这样一个例子:

#include <iostream>
#include <functional>

void qwer(const std::function<void()> &a)
{
        a();
}

int main()
{
        int x{0};

        auto f{
        [x]()mutable
        {
                std::cout << ++x << std::endl;
        }
        };

        qwer(f);
        qwer(f);
        qwer(f);

        return 0;
}

输出:

1
1
1

有2个问题:

  1. 为什么编译器不抛出错误?毕竟,函数qwer正在等待一个函数被传递给它,我们传递给它一个 lambda,据我所知,它不是一个函数。
  2. 为什么在传递f给函数时qwer,每次都会创建一个副本f?毕竟,在定义函数qwerusing 时,&我们明确指出我们要使用的不是对象的副本,而是对象本身
c++
  • 2 个回答
  • 10 Views
Martin Hope
VanechikSpace
Asked: 2022-03-17 23:58:01 +0000 UTC

使用结构的 std::array 数组初始化

  • 3

我想像这样初始化数组:

#include <string>
#include <array>

struct r{
        std::string name;
        int x;
};

int main()
{
        std::array<r, 2> a { {"weklrj", 3}, {"lekw", 2} };
}

但它给出了这个错误:

test.cpp: In function ‘int main()’:
test.cpp:12:50: error: too many initializers for ‘std::array<r, 2>’
   12 |  std::array<r, 2> a { {"weklrj", 3}, {"lekw", 2} };
      |                                                  ^

如果我再添加一个这样的括号,那么一切都会好起来的:

#include <string>
#include <array>

struct r{
        std::string name;
        int x;
};

int main()
{
        std::array<r, 2> a {{ {"weklrj", 3}, {"lekw", 2} }};
}

问题:这些额外的括号是做什么用的?事实上,根据第一个变体中的逻辑,外部是统一初始化,内部是每个单独结构的指定,一切都应该工作。

c++
  • 3 个回答
  • 10 Views
Martin Hope
VanechikSpace
Asked: 2022-03-14 20:06:32 +0000 UTC

引用函数指针

  • 0

这是它完美工作的方式:

#include <iostream>
#include <functional>

void qwer(std::function<void()> &x)
{
        x();
}

void e()
{
        std::cout << "hello" << std::endl;
}

int main()
{
        //void (*ee)() = e;
        std::function<void()> ee = e;
        qwer(ee);
}

但它不是这样的:

#include <iostream>
#include <functional>

void qwer(std::function<void()> &x)
{
        x();
}

void e()
{
        std::cout << "hello" << std::endl;
}

int main()
{
        void (*ee)() = e;
        //std::function<void()> ee = e;
        qwer(ee);
}

问题:

test2.cpp:18:7: error: cannot bind non-const lvalue reference of type ‘std::function<void()>&’ to an rvalue of type ‘std::function<void()>’
   18 |  qwer(ee);
      |       ^~
In file included from /usr/include/c++/9/functional:59,
                 from test2.cpp:2:
/usr/include/c++/9/bits/std_function.h:667:7: note:   after user-defined conversion: ‘std::function<_Res(_ArgTypes ...)>::function(_Functor) [with _Functor = void (*)(); <template-parameter-2-2> = void; <template-parameter-2-3> = void; _Res = void; _ArgTypes = {}]’
  667 |       function<_Res(_ArgTypes...)>::
      |       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
test2.cpp:4:34: note:   initializing argument 1 of ‘void qwer(std::function<void()>&)’
    4 | void qwer(std::function<void()> &x)
      |           ~~~~~~~~~~~~~~~~~~~~~~~^

他为什么写ee它是rvalue什么?毕竟,事实并非如此。

c++
  • 1 个回答
  • 10 Views
Martin Hope
VanechikSpace
Asked: 2022-03-13 18:34:59 +0000 UTC

命令行参数和条件中的空字符串

  • 2

在学习 c++ 时,我遇到了这个例子:

// В некоторых операционных системах argv[0] может быть просто пустой строкой, без имени программы
// Обрабатываем случай, когда argv[0] может быть пустым или не пустым
if (argv[0])
    std::cout << "Usage: " << argv[0] << " <number>" << '\n';
else
    std::cout << "Usage: <program name> <number>" << '\n';

这个例子让我陷入了昏迷。据我所知,指向空字符串的指针会一直存在,true所以else它永远不会被执行。要么我不明白,要么在这个例子中,错误不是空字符串,而是argv[0]指向的东西nullptr(据我所知,这些是不同的概念)

c++
  • 1 个回答
  • 10 Views
Martin Hope
VanechikSpace
Asked: 2022-03-13 17:40:40 +0000 UTC

条件为空字符串

  • 0

如果字符串为空,我想输出一个单词。这是它的工作原理:

#include <iostream>

int main()
{
        char q[]{'\0'};

        if(!q[0])
                std::cout << "hello" << std::endl;

        return 0;
}

但它不是这样的:

#include <iostream>

int main()
{
        char q[]{'\0'};

        if(!q)
                std::cout << "hello" << std::endl;

        return 0;
}

为什么?

c++
  • 1 个回答
  • 10 Views
Martin Hope
VanechikSpace
Asked: 2022-02-27 18:48:11 +0000 UTC

C++中的无限循环

  • 1
#include <iostream>
  
int main()
{
        int count(0);
        while (count < 10)
        {
                if (count == 5)
                        continue;
                std::cout << count << " ";
                ++count;
        }

        return 0;
}

我希望程序在进入无限循环之前打印出来1 2 3 4 ,但事实并非如此。我的周期马上开始。但是,如果我将行更改std::cout << count << " ";为 std::cout << count << " " << std::endl;,则输出将发生1 2 3 4 (但在列中)。问题:为什么在第一种情况下不显示数字?

c++
  • 1 个回答
  • 10 Views
Martin Hope
VanechikSpace
Asked: 2022-02-26 23:37:00 +0000 UTC

在 C++ 中使用已删除的函数

  • 0
#include <iostream>

struct qwer
{
        const int x{1};
};

qwer tt()
{
        qwer xx;
        return xx;
}

int main()
{
        qwer qq;
        qq = tt();

        return 0;
}

给出错误消息:

qwer.cpp: In function ‘int main()’:
qwer.cpp:18:10: error: use of deleted function ‘qwer& qwer::operator=(qwer&&)’
   18 |  qq = tt();
      |          ^
qwer.cpp:4:8: note: ‘qwer& qwer::operator=(qwer&&)’ is implicitly deleted because the default definition would be ill-formed:
    4 | struct qwer
      |        ^~~~
qwer.cpp:4:8: error: non-static const member ‘const int qwer::x’, can’t use default assignment operator

请解释是什么问题

c++
  • 1 个回答
  • 10 Views

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5