我正在 Linux 上编写 C 程序。然而,我结合了一些 C++ 来让生活变得更简单(正如教授和其他一些论坛所推荐的)。
我知道在尝试分配内存或从 C++ '#include' 库时会遇到一些问题。我遇到过几个类似的警告/错误,例如:
main.c:12:3: warning: implicit declaration of function âstrcpyâ [-Wimplicit-function-declaration]
strcpy(baseName,strBase.c_str());/*??*/
main.c:6:2: warning: implicit declaration of function âstrlenâ [-Wimplicit-function-declaration]
char *baseName = (char *)malloc(strlen("out") + 7);
^
main.c:6:34: warning: incompatible implicit declaration of built-in function âstrlenâ [enabled by default]
char *baseName = (char *)malloc(strlen("out") + 7);
^
main.c:11:3: error: unknown type name âstringâ
string string_base = "out";
^
main.c:11:24: warning: initialization makes integer from pointer without a cast [enabled by default]
string string_base = "out";
^
还有,
main.c:47:2: error: unknown type name ânode_tâ
node_t *root = buildTree(fp);
^
我将在下面提供我所拥有的代码,我被告知使用 C++ 中的 vector 可以处理添加新元素的内存分配,并且您可以看到这并不顺利。
我的问题是,“我应该做什么来编译我的代码?” '雷洛克'?甚至不尝试这条路线,...
主要:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
char buffer[100];
char *baseName = (char *)malloc(strlen("out") + 7);
FILE *fp;
if(argc < 2) {
string string_base = "out";
strcpy(baseName,strBase.c_str());/*??*/
fp = tmpfile();
if(fp == NULL) {
abort();
}
do
{
fputs(buffer, fp);
}while(fgets(buffer, sizeof(buffer), stdin));
rewind(fp);
}
else if(argc == 2) { /* P0 file */
char *file = (char *) malloc(strlen(argv[1] + 7));
strcpy(file, argv[1]);
strcpy(baseName, argv[1]);
strcat(file, ".fs182");
if((fp = fopen(argv[1], "r")) == NULL) {
printf("Unable to open file '%s'\n", argv[1]);
return 0;
}
}
else {
printf("%s", "Invalid number of arguments!\n");
return 1;
}
node_t *root = buildTree(fp);
char *preorderFile = (char *)malloc(strlen(baseName) + 17);
char *inorderFile = (char *)malloc(strlen(baseName) + 17);
char *postorderFile = (char *)malloc(strlen(baseName) + 17);
strcpy(preorderFile, baseName);
strcat(preorderFile, ".preorder");
strcpy(inorderFile, baseName);
strcat(inorderFile, ".inorder");
strcpy(postorderFile, baseName);
strcat(postorderFile, ".postorder");
printf("Preorder: ");
traversePreorder(root,0,preorderFile);
printf("\n");
return 0;
}
节点.h:
#ifndef NODE_H
#define NODE_H
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <fstream>
using namespacestd;
typedef struct node {
vector<string> list;
char nodeNum;
int buffsize;
struct node *left, *right;
}node_t;
void updated(node_t *, char[]);
#endif
最佳答案
I was told that using the vectors in C++ handles the memory allocation of adding a new element
确实如此,但 C++ vector 是 C++ 标准模板库的一个功能。它们只能在 C++ 中使用。
C 和 C++ 是不同的语言,具有相对较大的公共(public)子集。执行混合 C/C++ 编程需要使用 C++ 编译器构建 C++ 部分,使用 C 编译器构建 C 部分,以及两者之间的接口(interface)(它们进行通信的函数签名和数据类型)表达在公共(public)子集中并使用C规则进行链接。
虽然所有这些都是可行的,而且也不是太不寻常,但它比仅使用 vector
来获得内存管理自动化程度要节省的麻烦要多得多。任何善意推荐 vector
的知识渊博的人都建议您使用 C++ 而不是 C,而不是您尝试使用 vector
在 C 程序中。
My question is, "What should I do to compile my code?" 'relloc'? not even attempt this route,...
您正在考虑的函数名称是realloc
。无论如何,
根据您的经验水平,您应该选择一种语言(C 或 C++),并使用该语言完成整个项目。
无论哪种方式,每个源文件都必须使用
#include
header 提供其使用的所有库函数和数据类型的声明,例如malloc
和strlen
。有关隐式函数声明的警告是告诉您未能执行此操作的函数。 “未知类型名称”错误之一和其他一些警告源于同一问题。无论哪种方式,每个源文件都必须包含它使用的所有内部数据类型和函数的声明,例如
node_t
。通常,这是通过创建并#include 本地 header 来完成的。您提出的“未知类型名称”错误之一是由于未能执行此操作而导致的,并且它还可能导致隐式声明和其他警告或错误。动态内存管理的详细信息取决于您选择的语言。
关于c++ - 在编译器上使用 C 和 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52468267/