简体   繁体   中英

Using C and C++ on Compiler

I am writing a C program on Linux. However, I am incorporating some C++ to make life somewhat simpler (as recommended by a professor and some other forums).

I knew I would encounter some issues when trying to allocate memory or when I '#include' libraries from C++. I have come across several similar warnings/errors such as:

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";
                       ^

And also,

main.c:47:2: error: unknown type name ânode_tâ
  node_t *root = buildTree(fp);
  ^

I will provide code below of what I have, I was told that using the vectors in C++ handles the memory allocation of adding a new element and well as you can see that is not going over smoothly.

My question is, "What should I do to compile my code?" 'relloc'? not even attempt this route,...

Main:

#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;
}

node.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

That is true, but C++ vectors are a feature of the C++ standard template library. They can be used only in C++.

C and C++ are different languages with a relatively large common subset. Performing mixed C / C++ programming requires the C++ parts to be built with a C++ compiler, the C parts to be built with a C compiler, and the interface between the two -- the function signatures and data types by which they communicate -- to be expressed in the common subset and to use C rules for linkage.

Although all that is quite doable and not too unusual, it is a lot more hassle than you stand to save just by using vector to get a measure of memory management automation. Any knowledgeable person who recommended vector in good faith was recommending that you do so by use C++ instead of C, not that you try to use vector in a C program.

My question is, "What should I do to compile my code?" 'relloc'? not even attempt this route,...

The name of the function you're thinking of is realloc . Anyway,

  • At your level of experience, you should choose one language, either C or C++, and do the whole project in that language.

  • Either way, every source file must #include headers providing declarations of all the library functions and data types it uses, such as malloc and strlen . The warnings about implicit function declarations are telling you about functions for which you failed to do that. One of the "unknown type name" errors and some of the other warnings follow from the same problem.

  • Either way, every source file must contain declarations of all the internal data types and functions it uses, too, such as node_t . Typically that is accomplished by creating and #include ing a local header. One of the "unknown type name" errors you present results from failing to do that, and it could also give rise to implicit declaration and other warnings or errors.

  • Details of what you should do for dynamic memory management depend on which language you choose.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM