gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:37:45 25 4
gpt4 key购买 nike

你好,我创建了四个文件 fn.cpp header.h、main.cpp 和 makefile我收到两个错误请帮助修复它。

  1. fn.cpp:1 错误:未在此范围内声明字符串?为什么?

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

标题.h:

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

主要.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> 中定义 header 。包括它而不是 C 库的 <string.h>标题。

此外,您还需要包括 "header.h"来自两个源文件。

最后,你不能传递 string直接反对 printf ;那是一个对 C++ 类一无所知的 C 函数。要么使用 C++ I/O:

std::cout << ss;

或者使用 C 风格的字符串:

printf("%s", ss.c_str());

关于c++ - make 文件抛出以下两个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26525730/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com