$ 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 ++报告的那样,是否存在额外的括号对的一些意义/含义?
码
#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++样式演员的新手,需要帮助理解下面的代码是如何工作的(这是我写的一些虚拟代码来理解事物).
#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) 这适用于MSVC
#define Get64B(hi, lo) ((((__int64)(hi)) << 32) | (unsigned int)(lo))
Run Code Online (Sandbox Code Playgroud)
具体来说,'operator <<'的作用是什么?
谢谢你的帮助
我有一个文本文件(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)
哪里出错了?
我有一个文件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)
在一个数组中,浮点值在另一个数组中.
当数据长度未知时,如何读入文件?
如标题中所示,数组的每个元素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)
提前致谢.
对于此代码:
#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) 这有效:
#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)
第二段代码不起作用,但我找不到足够好的理由.对我来说,代码看起来太奇怪了.
但是在这种情况下只允许一次隐式转换的实际原因是什么?它是一个像这样唯一的语言功能吗?
我做了一个程序来查找一个数字是否属于斐波那契系列,如果它确实是什么位置.当我输入一个数字时,如果条件出错了.
#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++ ×6
c ×3
file-io ×2
matlab ×2
arrays ×1
c++11 ×1
constructor ×1
endianness ×1
fibonacci ×1
formatting ×1
g++ ×1
input ×1
macros ×1
operators ×1
shared-ptr ×1
static-cast ×1
templates ×1