[英]make file is throwing below two error
Hello i have created four file fn.cpp header.h,main.cpp and makefile I am getting two error plz help to fix it. 您好,我创建了四个文件fn.cpp header.h,main.cpp和makefile,我得到两个错误的plz帮助修复它。
fn.cpp:1 error: string was not declared in this scope? fn.cpp:1错误:未在此范围内声明字符串? why?
为什么?
fn.cpp:2 error :expected ',' or ';' fn.cpp:2错误:预期为','或';' before '{' token?
在“ {”令牌之前?
header.h: header.h:
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
int fn(string);
main.cpp: main.cpp中:
#include "header.h"
string s= " hello world";
int main()
{
fn(s):
}
fn.cpp: fn.cpp:
int fn(string ss)
{
printf("%s",ss);
}
makefile: 生成文件:
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
The std::string
class is defined in the <string>
header. std::string
类在<string>
标头中定义。 Include that instead of the C library's <string.h>
header. 包括它而不是C库的
<string.h>
标头。
Also, you need to include "header.h"
from both source files. 另外,您还需要在两个源文件中都包含
"header.h"
。
Finally, you can't pass a string
object directly to printf
; 最后,您不能将
string
对象直接传递给printf
; that's a C function which knows nothing of C++ classes. 那是一个C函数,对C ++类一无所知。 Either use C++ I/O:
使用C ++ I / O:
std::cout << ss;
or faff around with C-style strings: 或使用C风格的字符串:
printf("%s", ss.c_str());
many small "out of c++ style" problems :) 许多小的“ c ++风格之外的”问题:)
use header #include <string>
and avoid printf
if you can, better use cout
使用头文件
#include <string>
并避免使用printf
,最好使用cout
c++ lovers would like this a little bit more: C ++爱好者更喜欢:
fn.h fn.h
#include<string>
void fn(const std::string&);
fn.cpp fn.cpp
#include <stdio.h>
#include "fn.h"
void fn(const std::string& ss)
{
printf(ss.c_str());
}
hello.cpp HELLO.CPP
#include "fn.h"
std::string s = " hello world";
int main()
{
fn(s);
}
Makefile Makefile文件
all: hello
hello: hello.cpp fn.o
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.