[英]Using C and C++ on Compiler
我正在 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++ 中的向量处理添加新元素的内存分配,并且您可以看到这并不顺利。
我的问题是,“我应该怎么做才能编译我的代码?” '雷洛克'? 甚至不尝试这条路线,...
主要的:
#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
有人告诉我,在 C++ 中使用向量处理添加新元素的内存分配
确实如此,但 C++ 向量是 C++ 标准模板库的一个特性。 它们只能在 C++ 中使用。
C 和 C++ 是不同的语言,具有相对较大的公共子集。 执行混合 C/C++ 编程需要使用 C++ 编译器构建 C++ 部分,使用 C 编译器构建 C 部分,以及两者之间的接口——它们通信的函数签名和数据类型——以以公共子集表示并使用 C 规则进行链接。
尽管所有这些都非常可行且并不太罕见,但仅通过使用vector
来衡量内存管理自动化比您所忍受的要麻烦得多。 任何善意推荐vector
知识渊博的人都建议您使用 C++而不是C,而不是您尝试在 C 程序中使用vector
。
我的问题是,“我应该怎么做才能编译我的代码?” '雷洛克'? 甚至不尝试这条路线,...
您正在考虑的函数名称是realloc
。 反正,
根据您的经验水平,您应该选择一种语言,C 或 C++,并使用该语言完成整个项目。
无论哪种方式,每个源文件都必须#include
标头提供它使用的所有库函数和数据类型的声明,例如malloc
和strlen
。 关于隐式函数声明的警告告诉您有关您未能执行此操作的函数。 “未知类型名称”错误之一和其他一些警告来自同一问题。
无论哪种方式,每个源文件都必须包含它使用的所有内部数据类型和函数的声明,例如node_t
。 通常,这是通过创建和#include
本地标头来完成的。 您提出的“未知类型名称”错误之一是由于未能做到这一点,它还可能导致隐式声明和其他警告或错误。
您应该为动态内存管理做什么的详细信息取决于您选择的语言。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.