在尝试了Perl和一点C之后,我正在尝试学习C++,并且我已经陷入了细节和陷阱.考虑一下: -
int x = 1;
{
int x = x; // garbage value of x
}
int const arr = 3;
{
int arr[arr]; // i am told this is perfectly valid and declares an array of 3 ints !!
}
Run Code Online (Sandbox Code Playgroud)
嗯,为什么区别?
澄清:使用相同的名称在一种情况下有效,在另一种情况下无效.
我使用python创建了一个XML文件.但XML声明只有版本信息.如何使用XML声明包含编码,如:
<?xml version="1.0" encoding="UTF-8"?>
Run Code Online (Sandbox Code Playgroud) 是int a;C中的声明或定义?C++有什么区别吗?
我一直认为这只是一个宣言,直到今天......
这是宣言还是定义?
是否可以将函数参数定义为多种可能的类型?例如,函数可以使用字符串或整数来完成某些操作.有可能以某种方式定义它吗?
function something(int|string $token) {}
Run Code Online (Sandbox Code Playgroud)
或者每个参数只支持一种类型?
(请注意,我知道我以后可以过滤输入,我只想输入我的参数)
通常的函数指针定义形式是:
int function(int, int);
int (*ptr)(int, int);
Run Code Online (Sandbox Code Playgroud)
但我今天看到一张我不明白的表格.有人能解释一下吗?
int (*close) __P((struct __db *));
Run Code Online (Sandbox Code Playgroud) 我搜索了前方声明,并没有看到任何方法让我的情况有效.所以这里是:
1)有一个C头文件,一个导出接口,可以说是一个包含枚举类型的大型多组件软件
"export.h":
// This is in "C"!
typedef enum _VM_TYPE {...., ...., ...,} VM_TYPE;
Run Code Online (Sandbox Code Playgroud)
2)C++中的部分代码使用该导出.
"cpp_code.cpp":
// This is in C++
#include "export.h"
#include "cpp_header.hpp"
{ .... using VM_TYPE values to do stuffs....}
Run Code Online (Sandbox Code Playgroud)
"cpp_header.hpp":
// Need to somehow forward declear VM_TYPE here but how?
Struct VM_INFO {
....
VM_TYPE VType; //I need to add this enum to the struct
....
};
Run Code Online (Sandbox Code Playgroud)
很明显,问题出在cpp_head.hpp中,因为它不知道枚举.
我尝试添加到cpp_header.hpp
typedef enum _VM_TYPE VM_TYPE;
Run Code Online (Sandbox Code Playgroud)
它实际上会起作用.那为什么这个有用呢?因为它有C风格的语法?!无论如何,我被告知不要通过上层"管理"这样做("它是C++,而不是C").
是否还有其他方法可以根据当前的事物联系方式完成这项工作?他们不想更改/添加包含文件; "枚举类"只是c ++,对吗?仅将"enum VM_TYPE"添加到cpp_header.hpp将会出现有关重新定义的错误. …
我正在尝试使用 typedef 来理解函数声明。
这段代码在 C++ 中有什么作用?
typedef void fcn_t(void);
typedef void (*ptr_t)(void);
fcn_t f;
fcn_t *pf;
ptr_t pf2;
Run Code Online (Sandbox Code Playgroud)
以我的理解:
fcn_t是函数的类型,所以用 withf是函数声明(不是定义),以后我可以像定义它一样定义它,就像void f(void) { blabla(); bleble(); }我声明了void f(void);而不是fcn_t f;;fcn_t *是函数指针的类型,行pf是指针变量定义,并且pf是默认初始化的(假设代码摘录来自全局范围);fcn_t*和之间没有区别ptr_t,因此我所说的一切都pf适用于pf2。我做对了吗?如果我标记它们,这三个声明中的任何一个都会改变其含义extern吗?如果将代码编译为 C 而不是 C++ 会发生什么变化?
我有一个手写数组来填充我班上的表格,现在我从 ngOnInit 上的 JSON 中获取这个数组的内容,但它的结构不是我需要的。
所以我正在尝试编写一个函数,用我在 ngOnInit 上得到的这个新函数来填充表数组。
问题是,当我在我的 TS 类中的函数之外编写代码时,我收到此错误“函数实现丢失或未立即跟在声明之后”。
为什么会这样,可以做些什么来解决这个问题?
TS
export class MyComponent implements OnInit {
users: Object;
constructor(private tstService: MyComponentService) { this.source = new LocalDataSource(this.data) }
ngOnInit(): void {
this.tstService.getTstWithObservable()
.map(result => result.map(i => i.user.data))
.subscribe(
res => { this.users = res; }
);
}
console.log(this.users); // Here, just an example. Throws 'Function implementation is missing or not immediately following the declaration'
data = [
{
title: 'Monthly',
sdate: '01/04/1990',
edate: '30/09/1990',
},
];
source: LocalDataSource; …Run Code Online (Sandbox Code Playgroud) 我想将我的 JavaScript 函数文档分成 TypeScript .d.ts 文件。
例如:
components/
Button/
Button.jsx # JavaScript component
Button.d.ts # TypeScript documentation with prop types
Run Code Online (Sandbox Code Playgroud)
同样,Material UI 是如何做到这一点的。https://github.com/mui-org/material-ui/tree/master/packages/material-ui/src/Button
我的问题是 TypeScript 和 VSCode 无法识别.d.ts当前 JavaScript 文件的文件。
在我的设置中,我有以下Button.d.ts文件:
interface Props {
text: string
onClick: () => void
}
declare const Button: (props: Props) => Element
export default Button
Run Code Online (Sandbox Code Playgroud)
和以下Button.jsx文件:
import React from 'react'
const Button = ({ text, onClick }) => {
return <button onClick={onClick}>{text}</button>
}
export default Button
Run Code Online (Sandbox Code Playgroud)
但是 …
我在许多课程和示例中看到了许多不同的typedef用法C。
这是执行此操作的正确方法(来自ISO/IEC C 语言规范草案的示例)
typedef struct tnode TNODE;
struct tnode {
int count;
TNODE *left, *right;
};
TNODE s, *sp;
Run Code Online (Sandbox Code Playgroud)
但是我看到很多具有以下模式的代码:
typedef struct {
int a;
int b;
} ab_t;
Run Code Online (Sandbox Code Playgroud)
由于某些原因,这是错误的,还是正确的,但它提供了这样定义的结构的有限功能?