好吧我不是一个非常有经验的C++程序员,但我想知道下面的构造函数的参数中下划线的意义是什么?
class floatCoords
{
public:
floatCoords(float _x, float _y, float _width, float _height)
: x(_x), y(_y), width(_width), height(_height)
{
}
float x, y, width, height;
...
Run Code Online (Sandbox Code Playgroud) 关于在C++代码中多次包含头文件,我遇到了问题.
比方说,我有三个等级X,Y,Z.X和Y派生自Base类Z.我想在Y中创建一个X实例.代码将如下所示.
class Z { …some code… };
class X: public Z { …some code… }; //here #include header of class Z added
class Y: public Z //here #include header of class Z added as well as of X class
{
private:
X* mX; //instance of X
…some code…
};
Run Code Online (Sandbox Code Playgroud)
所以在这个基类的所有方法的多重定义中出现了.我怎么能解决这个问题
我对此Misra警告有两个用例,如下所述。编译器是否为#if定义保留了某些名称或特定名称而不能使用?
目前,我已禁用此警告,//lint !e9071但是我们真的需要对此类警告采取任何措施吗?如果我们禁用此类警告,将会有任何影响或风险吗?
情况1:
#ifndef __CCPPAR_H__
#define __CCPPAR_H__ //lint !e9071
#include "Ccp_Cfg.h"
#endif /* multiple inclusion protection - __CCPPAR_H__ */
Run Code Online (Sandbox Code Playgroud)
观察到的警告:
Run Code Online (Sandbox Code Playgroud)defined macro '__CCPPAR_H__' is reserved to the compiler [MISRA 2012 Rule 21.1, required]
情况2:
#ifndef __CCP_H__
#define __CCP_H__ //lint !e9071
#include "Ccppar.h"
#define MAJOR 0x02
#define MINOR 0x01
/* other parts of code */
#endif
Run Code Online (Sandbox Code Playgroud)
观察到以下Misra警告:
Run Code Online (Sandbox Code Playgroud)defined macro '__CCP_H__' is reserved to the compiler [MISRA 2012 Rule 21.1, required]
我正在读书C programming a modern approach.我发现了一个问题:
为什么标识符包含多个相邻的下划线并不是一个好主意(例如,在current__balance中)?
任何人都可以向我解释为什么会如此?
我查看了C编程语言的不同源代码,发现#define的使用风格不同。
我从技术上知道他们的用例,不管它们的样式如何,它们都可以按预期工作,但是从编码准则出发,我想问一下它们的含义以及何时使用特定的。
#define MY_DEFINE_H
#define MY_DEFINE_H_
#define MY_DEFINE_H__
#define __MY_DEFINE_H__
Run Code Online (Sandbox Code Playgroud)
另外,如果可以的话,请分享一些参考,以便我详细介绍。
I keep getting a compiler issue when trying to use a struct I defined in a header file.
我有两个文件main.c:
#include <stdio.h>
#include <stdlib.h>
#include "node.h"
int main(){
struct NODE node;
node.data = 5;
printf("%d\n", node.data);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
以及node.h:
#ifndef NODE
#define NODE
struct NODE{
int data;
struct NODE *next;
};
#endif
Run Code Online (Sandbox Code Playgroud)
我正在写一个小程序来用C进行一些模块化编程,但是却遇到了以下编译器错误:
node.h:5:21: error: expected ‘{’ before ‘*’ token
struct NODE *next;
^
Run Code Online (Sandbox Code Playgroud)
我得到了main.c编译,做我想它的时候,我定义做了什么struct,直接在文件中main.c,但由于某种原因,它不会工作,如果我把一个头文件中的定义,然后尝试把它列入main.c。这非常令人沮丧,我敢肯定这是一件小事,但是有人可以告诉我为什么这不起作用吗?从我一直在阅读的书中,我应该能够做到这一点,不是吗?
非常感谢!
有一个long double用于80bit浮点的类型,x87扩展具有使用此数据类型的指令。
我知道__float128编译器会模拟这种类型的基本操作。但是,无需模拟80位浮点运算。那么目的是__float80什么?
我猜有些架构没有80位浮点数据类型,__float80就像跨平台类型一样。如果CPU能够以80bit fp工作,则long double表示与相同__float80。如果long double不存在,则不存在或引用其他内容,并且编译器模拟80位fp操作,因此只能__float80用于表示80位浮点。
我对吗?你能给我看看一些例子吗?
我是C编程的新手并试图写一个简单的例子.我试图在类型实现上进行抽象,并简单地使用typedef和指定我可以对此类型执行的操作.我知道那时候类型是不完整的,但我打算把它完成到c-file,而不是标题.就这个:
test.h
#ifndef _TEST_H
#define _TEST_H
typedef my_type_t;
void init(my_type_t **t);
#endif //_TEST_H
Run Code Online (Sandbox Code Playgroud)
test.c
#include <stdlib.h>
#include "test.h"
// implementation details
struct my_type_t{ //<---- completening my_type_t to be a struct with 1 field
int field;
};
void init(struct my_type_t **t){ //<--- error: conflicting type for init
*t = malloc(sizeof(struct my_type_t));
(*t) -> field = 42;
}
Run Code Online (Sandbox Code Playgroud)
这样的事情可能吗?我希望实现完全隐藏有关实际类型定义的所有细节,只暴露可以用它完成的操作.
UPD:如果我们重写c-file如下:
#include <stdlib.h>
#include "test.h"
struct internal_my_type_definition_t{
int field;
};
void init(my_type_t **t){
struct internal_my_type_definition_t *st = …Run Code Online (Sandbox Code Playgroud) 作为一个简单的初学者项目,我正在尝试构建一个链表。我为节点创建了一个结构 typedef。我的问题是我想要另一个 typedef 作为指向列表节点的指针,出于某种原因,这不起作用,我很困惑。我在这里阅读了其他答案,但没有一个有效。
主文件
#include <stdio.h>
#include "linkedList.h"
int main(int argc, char **argv){
return 0;
}
Run Code Online (Sandbox Code Playgroud)
链表.h
#ifndef linkedList
#define linkedList
typedef struct listStrNode {
char *string;
struct listStrNode *next;
} listStringNode;
typedef listStringNode *linkedList;
#endif
Run Code Online (Sandbox Code Playgroud)
错误
In file included from main.c:3:
linkedList.h:9:35: error: expected identifier or ‘(’ before ‘;’ token
9 | typedef listStringNode *linkedList;
|
Run Code Online (Sandbox Code Playgroud)
编译:
gcc main.c
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
为什么当字节数malloc很大时函数的返回值会变化?
函数.c:
\n#include "func.h"\n\nint *func()\n{\n int *ptr = (int *)malloc(100000000);\n printf("ptr in func is %p \\n", ptr);\n return ptr;\n}\nRun Code Online (Sandbox Code Playgroud)\n函数.h:
\n#ifndef _FUNC_H\n#define _FUNC_H\n#include <stdio.h>\n#include <stdlib.h>\n//int *func();\n#endif\nRun Code Online (Sandbox Code Playgroud)\n主程序
\n#include <stdio.h>\n#include "func.h"\n\nint main(int argc, char** argv)\n{\n int *ptr = func();\n printf("ptr is %p \\n", ptr);\n}\nRun Code Online (Sandbox Code Playgroud)\n编译并运行:
\ngcc -g main.c func.c -o main\nRun Code Online (Sandbox Code Playgroud)\n编译结果为:
\nmain.c: In function \xe2\x80\x98main\xe2\x80\x99:\nmain.c:7:13: warning: implicit declaration of function \xe2\x80\x98func\xe2\x80\x99; did you mean \xe2\x80\x98putc\xe2\x80\x99? [-Wimplicit-function-declaration]\n int* ptr …Run Code Online (Sandbox Code Playgroud) c ×9
c++ ×3
header-files ×2
struct ×2
arguments ×1
coding-style ×1
constructor ×1
header ×1
identifier ×1
misra ×1
pointers ×1
syntax ×1
typedef ×1
x86 ×1