这两个构造函数有什么区别?
int x, y; //position
BasePoint(int px, int py) : x(px), y(py) {}
Run Code Online (Sandbox Code Playgroud)
和
int x, y; //position
BasePoint(int px, int py)
{
x = px;
y = py;
}
Run Code Online (Sandbox Code Playgroud)
什么x(px), y(py)叫?我什么时候使用这种类型的变量初始化?
谢谢.
我使用 STLpriority_queue并提供一个自定义比较器类,其构造函数接收指向存储优先级的向量的指针,因此 -
#include <iostream>
#include <queue> // std::priority_queue
#include <vector> // std::vector
using namespace std;
class CompareReachDist
{
const vector<float> *reach_dists;
public:
CompareReachDist(const vector<float> *input)
{
reach_dists = input;
}
bool operator() (const size_t &l, const size_t &r) const
{
return (reach_dists->at(l) > reach_dists->at(r));
}
};
typedef priority_queue<size_t, vector<size_t>, CompareReachDist> pq;
vector<float> reach_dists;
int main()
{
pq seeds(CompareReachDist(&reach_dists));
bool isEmpty = seeds.empty();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,在编译时出现错误:
error: request for member 'empty' in 'seeds', which is of non-class …Run Code Online (Sandbox Code Playgroud) c++ syntax initialization variable-initialization most-vexing-parse
我有一个静态的“init”变量,可以在启动时运行一次函数(RTOS),但它似乎初始化为一个随机值。如果我删除静态标签,一切正常。(除了它每次通过都运行 init 函数的明显问题。)任何人都可以更深入地了解为什么这不起作用,或者可能是实现这一目标的更好方法?
示例代码:
void ManageStructures()
{
// Variable declarations/definitions
static uint8_t StructInitialized;
// Have also tried "static uint8_t StructInitialized = 0", neither worked
// Function prototypes
void InitStruct();
if (!StructInitialized)
{
StructInitialized= 1;
InitStruct();
}
Test = StructInitialized;
Run Code Online (Sandbox Code Playgroud)
编辑:对于缺乏信息,我深表歉意。这是针对一家公司的,我正在努力遵守我们的公共信息政策。MCU是STM32F7系列,使用“Ac6 STM32 MCU GCC”工具链。我不太精通编译器操作,所以我可能需要更长时间才能找到编译器或 makefile 相关问题的答案。
编辑:很明显,这是编译器或链接器脚本的问题,而不是我的代码。话虽如此,但在找到这个问题的根源之前,我需要更多地了解工具链、链接器脚本和编译器,这一点也变得非常清楚。一旦我足够熟悉可以提供有价值的反馈或自己回答这个问题,我就会回到这个问题。谢谢大家的反馈和指导!
所以我在Delphi 2007工作,我正在清理我的代码.我注意到在很多程序中我声明了许多相同类型的不同变量.
例如我现在正在查看的一个程序我声明了4个不同的字符串列表,我必须var1 := TStringList.Create为每个列表输入.
我有想法制作一个程序,它接受一个开放的变量数组,我的4个变量列表,然后创建它们.电话会是这样的
CreateStringLists([var1,var2,var3,var4]);
Run Code Online (Sandbox Code Playgroud)
但据我所知,你不能通过引用传递开放数组,因此不能做我希望的事情.有没有人对此有任何有趣的想法?
delphi delphi-2007 variable-initialization open-array-parameters
以下代码有错误:
class A
{
private final String val;
public A(){
this.val = null;
}
public A(String val){
this();
this.val = val;
}
}
Run Code Online (Sandbox Code Playgroud)
错误是“可能已经分配了变量 val
是否有解决此错误的方法,而无需重新编写可能在默认构造函数中的任何代码?这是一个最小的工作示例;如果你问自己“默认构造函数中有哪些代码”,请记住,一个真实的例子可能有很多你不想在其他构造函数中重复的代码(分配其他最终变量等)。
还请记住,这是一个最小的例子,同样的问题存在于大量的构造函数中。
java constructor variable-initialization constructor-chaining
我正在阅读Stephen G. Kochan撰写的一本名为"Objective-C编程"的书,第六版.它在第144页上有以下声明,这使我感到困惑:
作为基本C数据类型的局部变量没有默认初始值,因此在使用它们之前必须将它们设置为某个值.
然而,当我有以下代码时,它仍然有效,并显示0:
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
int number;
NSLog(@"%i", number);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
是不是int基本的C数据类型?
任务是while仅使用循环打印以下形状.
*
**
***
****
*****
******
*******
********
*********
Run Code Online (Sandbox Code Playgroud)
以下代码是我已经尝试过的,但不幸的是它不起作用:
#include "stdafx.h"//Visual Studio 2015
#include <stdio.h>
#include <stdlib.h>// using for command system("pause") ;
#include <math.h>
int main()
{
int i=0, k=0;
while (i < 10)
{
while (k <= i)
{
printf("*");
k++;
}
printf("\n");
i++;
}
system("pause");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我不能自己调试它.有人可以为我调试这个吗?
我知道这很简单,但我很困惑是否int a (5);比int a=5;C++更快,正如我在某处读到的那样,如果在类中定义了只有一个参数的构造函数,则可以使用等号进行初始化。(语句可以用等号写)
所以我想可能是第一个是额外的工作。
首先,看看下面的简单代码:
char str[80] = "This is - my text - for test";
const char s[2] = "-";
char *token;
token = strtok(str, s);
while (token != NULL) {
printf(" %s\n", token);
token = strtok(NULL, s);
}
Run Code Online (Sandbox Code Playgroud)
该函数strtok()返回数据类型char*,如您所见,我们创建了一个名为token该变量未初始化的变量.
现在,看看下一个代码:
char *buff;
int num = 500;
sprintf(buff, "%d", num);
Run Code Online (Sandbox Code Playgroud)
上一个代码的结果是错误uninitialized local variable 'buff'.
我的问题是,为什么在第一个代码中没有出现任何问题,而在第二个代码中发生了错误?
c++ ×4
c ×3
constructor ×3
delphi ×1
delphi-2007 ×1
embedded ×1
int ×1
java ×1
methods ×1
objective-c ×1
oop ×1
performance ×1
syntax ×1
while-loop ×1