C++ make文件抛出以下两个错误
您好,我已经创建了四个文件fn.cpp header.h、main.cpp和makefile 我得到两个错误plz帮助来修复它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
#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”
<>最后,不能将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