小编Laz*_*zer的帖子

为什么g ++将函数报告为类型为int()()?

$ cat -n cons.cpp
     1  #include <iostream>
     2
     3  using namespace std;
     4
     5  int return1() {
     6      return 1;
     7  }
     8
     9  int main() {
    10      cout<< return1.m_one << endl;
    11      return 0;
    12  }
$ g++ cons.cpp
cons.cpp: In function 'int main()':
cons.cpp:10: error: request for member 'm_one' in 'return1',
             which is of non-class type 'int ()()'
$
Run Code Online (Sandbox Code Playgroud)

也许这是编译器特定的,但是int ()()如上面的g ++报告的那样,是否存在额外的括号对的一些意义/含义?

c++ g++

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

为什么我的模板专业化不起作用?

#include<iostream>
using namespace std;

template<int N> void table(int i) {   // <-- Line 4
    table<N-1>(i);
    cout << i << " * " << N << " = " << i * N << endl;
}

template<1> void table(int i) {       // <-- Line 9
    cout << i << " * " << 1 << " = " << i * 1 << endl;
}

int main() {

    table<10> (5);                    // <-- Line 15
}
Run Code Online (Sandbox Code Playgroud)

预期产出

5 * 1 = …
Run Code Online (Sandbox Code Playgroud)

c++ templates

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

如何使用静态强制转换来管理共享对象的生命周期?

我是C++样式演员的新手,需要帮助理解下面的代码是如何工作的(这是我写的一些虚拟代码来理解事物).

#include <iostream>
#include <memory>

class A { 
public:
    A() : a(1) {
        std::cout << "Creating A\n";
    }   
    ~A() {
        std::cout << "Destroying A\n";
    }   
    int a;
};

class B : public A { 
public:
    B() : b(2) {
        std::cout << "Creating B\n";
    }   
    ~B() {
        std::cout << "Destroying B\n";
    }   
    int b;
};

int main() {
    std::shared_ptr<B> objectB(new B());
    {   
    std::shared_ptr<A> (static_cast<A*>(objectB.get()));
    std::cout << "End of inner scope\n";
    }   
    std::cout << "End of outer scope\n";
}
Run Code Online (Sandbox Code Playgroud)

它打印

Creating A …
Run Code Online (Sandbox Code Playgroud)

c++ shared-ptr static-cast c++11

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

这个C++宏有什么作用?

这适用于MSVC

#define Get64B(hi, lo) ((((__int64)(hi)) << 32) | (unsigned int)(lo))
Run Code Online (Sandbox Code Playgroud)

具体来说,'operator <<'的作用是什么?

谢谢你的帮助

c++ macros operators

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

这有什么问题[从Matlab中的文本文件中读取输入]?

我有一个文本文件(c:\ input.txt),它有:

2.0 4.0 8.0 16.0 32.0 64.0 128.0 256.0 512.0 1024.0 2048.0 4096.0 8192.0
Run Code Online (Sandbox Code Playgroud)

在Matlab中,我想把它读作:

data = [2.0 4.0 8.0 16.0 32.0 64.0 128.0 256.0 512.0 1024.0 2048.0 4096.0 8192.0]
Run Code Online (Sandbox Code Playgroud)

我试过这段代码:

fid=fopen('c:\\input.txt','rb');
data = fread(fid, inf, 'float');
data
Run Code Online (Sandbox Code Playgroud)

但我得到一些垃圾值:

data =

  1.0e-004 *

    0.0000
    0.0015
    0.0000
    0.0000
    0.0000
    0.0000
    0.0000
    0.0001
    0.0239
    0.0000
    0.0000
    0.0000
    0.0000
    0.0066
    0.0000
    0.0000
    0.0000
    0.0000
    0.0000
    0.0000
    0.0000
    0.0016
    0.0000
    0.0000
    0.0276
    0.0000
    0.3819
    0.0000
    0.0000
Run Code Online (Sandbox Code Playgroud)

哪里出错了?

formatting file-io matlab input

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

如何在MATLAB中读到文件结尾?

我有一个文件a.txt:

03,17.406199
05,14.580129
07,13.904058
11,14.685388
15,14.062603
20,14.364573
25,18.035175
30,21.681789
50,22.662820
Run Code Online (Sandbox Code Playgroud)

文件中的行数未知.我想读取文件和存储

3
5
7
11
15
20
30
50
Run Code Online (Sandbox Code Playgroud)

在一个数组中,浮点值在另一个数组中.

当数据长度未知时,如何读入文件?

arrays file-io matlab

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

如何生成一个8字节的数组,每个字节都是C中的随机数

如标题中所示,数组的每个元素iv应包含0到255之间的随机数.我尝试过:

char iv[8];
char buf[2];
int i, k;
srand(time(NULL));
for (i = 0; i <8; i++){
     k = rand()%256;
     iv[i] = (char)k;
}
Run Code Online (Sandbox Code Playgroud)

提前致谢.

c

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

这与endian-ness有什么关系吗?

对于此代码:

#include<stdio.h>

void hello() { printf("hello\n"); }
void bye()   { printf("bye\n");   }

int main() {
    printf("%p\n", hello); 
    printf("%p\n", bye);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我机器上的输出:

0x80483f4
0x8048408
Run Code Online (Sandbox Code Playgroud)

[第二个地址价值更大]

键盘上

0x8048541
0x8048511
Run Code Online (Sandbox Code Playgroud)

[第二个地址的价值较小]

这与机器的字节序有什么关系吗?如果不,

  • 为什么地址排序有所不同?

  • 另外,为什么区别差异?

    0x8048541 - 0x8048511 = 0x30

    0x8048408 - 0x80483f4 = 0x14


顺便说一下,我刚检查过.这段代码(取自这里)说这两台机器都是Little-Endian

#include<stdio.h>
int main() {    
    int num = 1; 
    if(*(char *)&num == 1)
        printf("Little-Endian\n");
    else    
        printf("Big-Endian\n");
    return 0;       
}
Run Code Online (Sandbox Code Playgroud)

c c++ memory-management function-pointers endianness

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

为什么只允许一个隐式转换将参数解析为C++中的函数?

这有效:

#include<cstdio>

class A{
public:
    A(int a):var(a){}
    int var;
};

int f(A obj) {
    return obj.var;
}

int main() {
    std::cout<<f(23);  // output: 23
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

虽然这不是:

#include<cstdio>

class A{
public:
    A(int a, int b):var1(a), var2(b){}
    int var1, var2;
};

int f(A obj) {
    return (obj.var1 + obj.var2);
}

int main() {

    cout<<f(22, 23); // error: conversion from 'int' to 
                     // non-scalar type 'A' requested
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

第二段代码不起作用,但我找不到足够好的理由.对我来说,代码看起来太奇怪了.

但是在这种情况下只允许一次隐式转换的实际原因是什么?它是一个像这样唯一的语言功能吗?

c++ language-features constructor language-design

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

斐波那契数!如果一直都是假的

我做了一个程序来查找一个数字是否属于斐波那契系列,如果它确实是什么位置.当我输入一个数字时,如果条件出错了.

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main(void)
{
    int i,x=1,y=1,z,num;
    clrscr();
    printf("Enter a number to find in fibonacci series:");
    scanf("%d",&num);
    /*to find if the number is a part of fibonacci series or not*/
    if((isdigit(sqrt(5*num*num+4)))||(isdigit(sqrt(5*num*num-4))))  //<-- this if!
    {//belongs to fibo!
        for(i=1;    ;i++)
        {
            if(x==num)
            break;
            z=x+y;
            x=y;
            y=z;
        }
        printf("%d is the %d term of fibonacci series.",num,i);
    }
    else
        printf("Dear user,The entered number is not a part of the fibonacci series.");

    getch();
}
Run Code Online (Sandbox Code Playgroud)

c fibonacci

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