小编Rem*_*eau的帖子

帕斯卡的功能步骤

我有以下代码,结果是42,但是为什么呢?答案必须是13,因为7+6=13

program HelloWorld;

function F (a : integer) : integer;
begin
  if (a = 1) or (a = 2) then
    F := 2
  else
    F := F(a-1) + F(a-2);
end;

begin
  WriteLn(F(8));
end.
Run Code Online (Sandbox Code Playgroud)

delphi pascal freepascal

-3
推荐指数
1
解决办法
199
查看次数

C++ 中的字符串不反转

我编写了一个程序来反转用户输入的字符串,但它不起作用。我使用Reverse the string in C++string reverse_name(name.rbegin(), name.rend())做到了这一点,但它不起作用并给了我错误:

从 'std::__cxx11::basic_string<char, 没有可行的转换,
      std::char_traits<char>、std::allocator<char> >::reverse_iterator'(又名
      'reverse_iterator<__normal_iterator<char *, std::__cxx11::basic_string<char> > >') 到
      'std::__cxx11::string'(又名'basic_string<char>')
  字符串reverse_word = (word.rbegin(), word.rend());

我的代码:

#include <iostream>
using namespace std;

int main()
{
  string word, reversed_word;
  cin >> word;

  reversed_word = (word.rbegin(), word.rend());
  cout << reversed_word;

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ c++11

-3
推荐指数
1
解决办法
670
查看次数

使用push_back将变量存储在向量中

我正在尝试使用push_back和for循环将部分和存储在向量中,但是由于某种原因push_back会导致无限循环。

cin >> n;

vector <int> partialSums(n);

for (i = 1; i <= partialSums.size(); ++i) {
    sum = sum + i;
    partialSums.push_back(sum);
    cout << sum << endl;
}

return 0;
Run Code Online (Sandbox Code Playgroud)

c++ vector push-back

-3
推荐指数
1
解决办法
48
查看次数

数组大小确定 C++17

我正在尝试使用该std::size()函数确定数组的大小,但我的编译器带来了此错误:

/root/Desktop/practise.cpp:9:34: 错误:“size”不是“std”的成员;你的意思是“size_t”?

这是第一个代码:

#include <iostream>
#include <array>

using namespace std;

int main()
{

    int values [] {2,3,4,5,6,7,8,9,10};

    cout <<"The array size is:"<< std::size(values);


    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在改变语句std::size(values)std::size_t(values),似乎并不准确弹出一个庞大的数字。输出的值为:

140725039324624

c++ c++17

-3
推荐指数
1
解决办法
83
查看次数

需要帮助理解为什么这个函数返回 null

我正在编写一个 Delphi Android 应用程序来使用 Google Push。

这是有问题的功能。它总是返回空值:

var
  PushService: TPushService;
  Notifications: TArray<TPushServiceNotification>;
  ServiceConnection: TPushServiceConnection;
begin
  PushService := TPushServiceManager.Instance.GetServiceByName(TPushService.TServiceNames.Gcm);
  Memo1.Lines.Add('PushService');
  if not assigned(PushService) then
    Memo1.Lines.Add('PushService error')
  else begin
    Memo1.Lines.Add('PushService ok');
    PushService.AppProps[TPushService.TAppPropNames.GCMAppID] := '543546532983';
    ServiceConnection := TPushServiceConnection.Create(PushService);
Run Code Online (Sandbox Code Playgroud)

delphi android firemonkey

-3
推荐指数
1
解决办法
118
查看次数

联合成员分配时的 C++ 分段错误

以下代码在分配给union成员时产生分段错误。这一定是误解工会的结果,但我无法指出我的错误。

Q1:是否有必要在 T1 的构造函数中为适当的联合成员变量包含一些初始化?

我尝试了类似于建议的解决方案(在下面的代码块中找到)来初始化适当的成员,但没有成功。

Q2:如果T1初始化没问题,为什么分配到这个位置会产生segmentation fault?

相关代码:

struct T1
{
    T1(int type)
    {
        this->type = type;
        //Q1 : need to init appropriate union struct here also? Something like:
        /*
        *if (type == 1)  
        *{
        *    union_member1.str = "";
        *}
        *else
        *{
        *    union_member2.value = 0;
        *}
        */
        //If so, better alternative to this?
    }

    int type;           //tells which union struct is used
    
    union               
    {
        struct              //type 1
        {
            std::string str;
        } union_member1;

        struct              //type 2
        {
            int …
Run Code Online (Sandbox Code Playgroud)

c++ segmentation-fault c++11

-3
推荐指数
1
解决办法
125
查看次数

C++ 从父级调用子函数

我试图从父级调用一个覆盖函数,发现它只是崩溃了。

这很难描述,所以这里是最小的可重现代码:

#include <iostream>

class A
{
public:
    A()
    {
        init1();
    }
    
    void init1()
    {
        printf("1");
        init2();
        printf("2");
    }

    virtual void init2() = 0;
};

class B : public A
{
public:
    B()
    : A()
    {}

    void init2() override
    {
        printf("hello");
    }
};

int main()
{
    B b;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在 MSVC 2019 上它崩溃了,在http://cpp.sh/ 上它输出“1”然后main()返回 0,但我们从未看到"hello""2"

  1. 为什么会崩溃?从低层次的角度来看会发生什么?是A试图调用它自己的init2()吗?

  2. 有没有办法做到这一点,所以我不必在每个派生类中添加init2()其构造函数?

c++ class

-3
推荐指数
1
解决办法
62
查看次数

为什么重新分配先前分配为 nullptr 的指针无效?

我是 C++ 新手,正在阅读《使用 C++ 的编程原理和实践》一书。我遇到了一些我无法完全理解的事情。希望有人能帮助我理解这一点。

为什么这是无效的?

int* p = nullptr;
*p = 7;
Run Code Online (Sandbox Code Playgroud)

c++ pointers nullptr

-3
推荐指数
1
解决办法
264
查看次数

C++ void函数

我是C++的新手.我有一个问题void,我似乎无法找到一个明确的答案.

正如我已阅读并理解,void不会返回任何价值.

但在这里,如果我说:

void display(int a, int b){
    cout << a+b;
}
Run Code Online (Sandbox Code Playgroud)

我在使用此功能时main():

display(20,30);
Run Code Online (Sandbox Code Playgroud)

是不是它的价值回归a+b

有人可以帮我理解背后的逻辑吗?

c++

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

双循环索引问题

我今天一直在学习Delphi,我面临一个奇怪的错误:

type
  TMatriz = array[1..10,1..10] of Integer;

var
  i, j: Integer;
  tablaDeMultiplicar: TMatriz;

begin
  for i := 1 to 10 do
  begin
    for j := 1 to 10 do
    begin
      tablaDeMultiplicar[i-1,j-1] := i*j;
    end;
  end;
  for i := 0 to 9 do
  begin
    for j := 0 to 9 do
    begin
      Write(tablaDeMultiplicar[i,j]:6);
    end;
    Writeln;
  end;
  Readln;
end.
Run Code Online (Sandbox Code Playgroud)

据我所知,这显示了乘法表.现在它显示一行1 2 ... 9 0,其余为0.

当我尝试将索引从1和10更改为0和9并按顺序调整行时,它什么也没有显示.我尝试了另外的东西,我只是这样工作:

程序很奇怪

我从这里得到的唯一结论是,你不能使用for的vars:/

delphi for-loop matrix

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