![](/img/trans.png)
[英]Why this below code is throwing an error "Abort signal from abort(3) (SIGABRT)"?
[英]make file is throwing below two error
您好,我創建了四個文件fn.cpp header.h,main.cpp和makefile,我得到兩個錯誤的plz幫助修復它。
fn.cpp:1錯誤:未在此范圍內聲明字符串? 為什么?
fn.cpp:2錯誤:預期為','或';' 在“ {”令牌之前?
header.h:
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
int fn(string);
main.cpp中:
#include "header.h"
string s= " hello world";
int main()
{
fn(s):
}
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
類在<string>
標頭中定義。 包括它而不是C庫的<string.h>
標頭。
另外,您還需要在兩個源文件中都包含"header.h"
。
最后,您不能將string
對象直接傳遞給printf
; 那是一個C函數,對C ++類一無所知。 使用C ++ I / O:
std::cout << ss;
或使用C風格的字符串:
printf("%s", ss.c_str());
許多小的“ c ++風格之外的”問題:)
使用頭文件#include <string>
並避免使用printf
,最好使用cout
C ++愛好者更喜歡:
fn.h
#include<string>
void fn(const std::string&);
fn.cpp
#include <stdio.h>
#include "fn.h"
void fn(const std::string& ss)
{
printf(ss.c_str());
}
HELLO.CPP
#include "fn.h"
std::string s = " hello world";
int main()
{
fn(s);
}
Makefile文件
all: hello
hello: hello.cpp fn.o
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.