在:http://www.learncpp.com/cpp-tutorial/19-header-files/
提到以下内容:
add.cpp:
int add(int x, int y)
{
return x + y;
}
Run Code Online (Sandbox Code Playgroud)
main.cpp中:
#include <iostream>
int add(int x, int y); // forward declaration using function prototype
int main()
{
using namespace std;
cout << "The sum of 3 and 4 is " << add(3, 4) << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我们使用了前向声明,以便编译器在编译时知道"
add"是什么main.cpp.如前所述,为要在其他文件中使用的每个函数编写前向声明可能会很快变得乏味.
你能进一步解释" 前瞻性宣言 "吗?如果我们在main()函数中使用它会有什么问题?
我见过default在类中的函数声明旁边使用过.它有什么作用?
class C {
C(const C&) = default;
C(C&&) = default;
C& operator=(const C&) & = default;
C& operator=(C&&) & = default;
virtual ~C() { }
};
Run Code Online (Sandbox Code Playgroud) 我正在寻找一种干净有效的方法来声明相同类型和相同值的多个变量.现在我有:
String one = "", two = "", three = "" etc...
Run Code Online (Sandbox Code Playgroud)
但我正在寻找类似的东西:
String one,two,three = ""
Run Code Online (Sandbox Code Playgroud)
这是在Java中可以做的事情吗?牢记效率.
我想像数组一样初始化一个向量.
例
int vv[2] = {12, 43};
Run Code Online (Sandbox Code Playgroud)
但是当我这样做的时候,
vector<int> v(2) = {34, 23};
Run Code Online (Sandbox Code Playgroud)
要么
vector<int> v(2);
v = {0, 9};
Run Code Online (Sandbox Code Playgroud)
它给出了一个错误:
在'{'标记之前预期的primary-expression
和
错误:预期','或';' 在'='之前
分别.
我刚刚进行了中间编程测试,其中一个问题我的错误如下:
函数声明后不需要分号(';').
对或错.
我选择了"假"(请纠正我,如果我错了因为我觉得我疯了),函数声明就是你在定义之前写的(在代码的顶部)所以编译器知道函数甚至在调用之前调用,函数定义是整个函数的组成部分.
即.
宣言:
int func();
Run Code Online (Sandbox Code Playgroud)
定义:
int func() {
return 1;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,这个答案不应该是假的吗?
在Apple的文档的参考部分中有很多这种事情的实例:
func runAction(_action: SKAction!)
Objective-C'等效'是:
- (void)runAction:(SKAction *)action
让我感到非常重要的是(在Swift参考中)下划线后面有一个空格,而"动作"用斜体字写成.
但我无法弄清楚这是想传达什么.所以也许问题是......参考文献中使用的约定是否有参考?
- 这是我参考下划线使用的参考页面:https: //developer.apple.com/documentation/spritekit/sknode#//apple_ref/occ/instm/SKNode/runAction
Swift 3对如何使用和命名函数/方法参数名称和参数标签进行了一些更改.这会对这个问题及其答案产生影响.@Rickster做了一个很棒的工作,回答了关于_underscores的一个不同的问题,在这些函数中清除了大部分内容,这里:为什么我需要在swift中使用下划线?
要在"同一时间"声明多个变量,我会这样做:
a, b = True, False
Run Code Online (Sandbox Code Playgroud)
但是,如果我必须声明更多的变量,它变得越来越不优雅:
a, b, c, d, e, f, g, h, i, j = True, True, True, True, True, False, True ,True , True, True
Run Code Online (Sandbox Code Playgroud)
有没有更好/优雅/方便的方法来做到这一点?
提前致谢!
编辑:
这必须是非常基本的,但是如果我使用列表或元组来存储变量,我将如何处理以便我有所帮助,因为:
aList = [a,b]
Run Code Online (Sandbox Code Playgroud)
无效,我必须这样做:
a, b = True, True
Run Code Online (Sandbox Code Playgroud)
或者我错过了什么?
我一直认为在C中,所有变量都必须在函数的开头声明.我知道在C99中,规则与C++中的规则相同,但C89/ANSI C的变量声明放置规则是什么?
以下代码使用gcc -std=c89和成功编译gcc -ansi:
#include <stdio.h>
int main() {
int i;
for (i = 0; i < 10; i++) {
char c = (i % 95) + 32;
printf("%i: %c\n", i, c);
char *s;
s = "some string";
puts(s);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
不应该在C89/ANSI模式下声明c并s导致错误吗?
我想编写可重用的代码,需要在开头声明一些变量并在脚本中重用它们,例如:
DEFINE stupidvar = 'stupidvarcontent';
SELECT stupiddata
FROM stupidtable
WHERE stupidcolumn = &stupidvar;
Run Code Online (Sandbox Code Playgroud)
如何声明一个变量并在后面的语句中重用它,比如使用SQLDeveloper.
尝试
BEGIN和END;.使用获取变量&stupidvar.DEFINE并访问变量.VARIABLE并访问变量.但我在尝试期间遇到各种错误(未绑定变量,语法错误,预期SELECT INTO...).