Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ make文件抛出以下两个错误_C++_String_Makefile - Fatal编程技术网

C++ make文件抛出以下两个错误

C++ make文件抛出以下两个错误,c++,string,makefile,C++,String,Makefile,您好,我已经创建了四个文件fn.cpp header.h、main.cpp和makefile 我得到两个错误plz帮助来修复它 fn.cpp:1错误:未在此作用域中声明字符串?为什么? fn.cpp:2错误:应为“,”或“;”在“{”标记之前 标题h: #include<iostream> #include<string.h> #include<stdio.h> using namespace std; int fn(string); fn.cpp: int

您好,我已经创建了四个文件fn.cpp header.h、main.cpp和makefile 我得到两个错误plz帮助来修复它

  • fn.cpp:1错误:未在此作用域中声明字符串?为什么?

  • fn.cpp:2错误:应为“,”或“;”在“{”标记之前

  • 标题h:

    #include<iostream>
    #include<string.h>
    #include<stdio.h>
    using namespace std;
    int fn(string);
    
    fn.cpp:

    int fn(string ss)
    {
        printf("%s",ss);
    }
    
    生成文件:

    all:hello
    hello:main.o fn.o
    tab   g++ main.o fn.o-o hello
    main.o:main.cpp
    tab  g++ -c main.cpp
    fn.o:fn.cpp
    tab g++ -c fn.cpp
    

    std::string
    类是在
    头中定义的。包含它而不是C库的

    此外,还需要从两个源文件中包括
    “header.h”

    <>最后,不能将字符串对象直接传递给<代码> Prtff;这是C++函数,对C++类一无所知。
    std::cout << ss;
    

    许多小的“C++风格”问题: 使用标题

    #包括
    并避免
    printf
    如果可以,最好使用
    cout

    c++爱好者更希望这样:

    fn.h

    生成文件

    all: hello
    
    hello: hello.cpp fn.o
    

    请把代码缩进。
    printf("%s", ss.c_str());
    
    #include<string>
    void fn(const std::string&);
    
    #include <stdio.h>
    #include "fn.h"
    
    void fn(const std::string& ss)
    {
        printf(ss.c_str());
    }
    
    #include "fn.h"
    
    std::string s = " hello world";
    
    int main()
    {
        fn(s);
    }
    
    all: hello
    
    hello: hello.cpp fn.o