第一次在一个项目中使用 .h 和多个 .cpp 文件时,错误:(

first time using .h and multiple .cpp files in one project, errorssss :(

本文关键字:cpp 文件 错误 一个 项目 第一次      更新时间:2023-10-16

StackOverflow上的第二篇文章。我只是有一些关于为什么我的程序以这种方式运行的一般性问题,我不想帮助完成它,我只是周五缺课,显然我错过了很多。我的任务是设计一个包含 3 个 .cpp 和 2 个 .h 文件的程序,本质上它将使用气泡排序、插入排序、选择排序方法以及顺序和二进制搜索来搜索和排序字符串数组。然后,我们应该对每种方法进行基准测试,以确定哪种方法最快。

只是对为什么编译器一直对我大喊大叫感到困惑,我已经坐在这里大约一个小时摆弄不同的选项或以不同的方式输入代码但无济于事,这没有多大意义。

我的头文件

const int NOT_FOUND = -1;
int sequentialSearch(string a[], string needle, int length );

约翰搜索.cpp

#include "JohnSearch.h"
#include <string>
int sequentialSearch(string copied[], string needle, int length)
{
int i;   // iteration variable
// look at every element to see if it is the same as needle
for( i = 0; i < length; i++ )
    if( copied[i] == needle )
        return i;
return NOT_FOUND;
}

测试搜索.cpp

#include "JohnSearch.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
/*
** printArray(title,a,length): print out title and then the contents of array a
*/
void printArray(string title, string ref[], int length )
{
    int i;      // array iteration
    cout << title << ": n";
    for( i = 0; i < length; i++ )
        cout<<setw(20)<<ref[i]<<"n";
}
int main(void)
{
string reference[]={"John", "Allen", "Kevin", "Elisabeth"};
const int SZ=sizeof(reference)/sizeof(reference[0]);
string copied[SZ];
printArray("Reference", reference, SZ); 

// sequential search (on unsorted array)
cout<<"Search.sequential(ref,Kevin):t"<<sequentialSearch(reference, "Kevin", SZ)<<endl;

system("Pause");
return 0;

}

错误

johnsearch.h(2): error C2065: 'string' : undeclared identifier
johnsearch.h(2): error C2146: syntax error : missing ')' before identifier 'a'
johnsearch.h(2): error C2059: syntax error : ')'
testjohnsearch.cpp(28): error C3861: 'copyArray': identifier not found
testjohnsearch.cpp(31): error C2064: term does not evaluate to a function taking 3 arguments
johnsearch.h(2): error C2065: 'string' : undeclared identifier
johnsearch.h(2): error C2146: syntax error : missing ')' before identifier 'a'
johnsearch.h(2): error C2059: syntax error : ')'
johnsearch.cpp(7): error C2065: 'string' : undeclared identifier
johnsearch.cpp(7): error C2146: syntax error : missing ')' before identifier 'copied'
johnsearch.cpp(7): error C2374: 'sequentialSearch' : redefinition; multiple initialization
johnsearch.h(2) : see declaration of 'sequentialSearch'
johnsearch.cpp(7): error C2059: syntax error : ')'
johnsearch.cpp(8): error C2143: syntax error : missing ';' before '{'
johnsearch.cpp(8): error C2447: '{' : missing function header (old-style formal list?)

我显然在做一些完全完全错误的事情。我需要JohnSearch.cpp JohnSearch.h,对吧?JohnSearch.h 中函数的前向声明是在 JohnSearch 中定义的.cpp所以我需要这两个文件吗?

我真的很困惑。我们应该修改的示例程序有 2 个 .h 文件和 3 个 .cpp 个文件。其中 2 个.cpp文件对应于 2 个 .h 文件,所以这就是为什么我认为我还需要 2 个 .h 文件和 3 个 .cpp 文件。

字符串仍未定义。

johnsearch.h(2):错误 C2065:"字符串":未声明的标识符

您的头文件使用 string ,因此您需要在声明之前包含 <string> 。您还需要将其限定为 std::string,因为字符串类驻留在 std 命名空间中

因此,您的头文件变为:

#include <string>
const int NOT_FOUND = -1;
int sequentialSearch(std::string a[], std::string needle, int length );

(您还应该在头文件中使用包含保护)

同样,您的 JohnSearch.cpp 也使用字符串,因为string位于 std 命名空间中,如果不使用 std::string,则会收到错误

在你的TestSearch.cpp中,你的顶部有一个using namespace std;,你也可以在JohnSearch中做同样的事情.cpp这样你就可以使用string而不是std::string

如有疑问,请简化。您可以将代码归结为如下所示:

#include "JohnSearch.h"
void sequentialSearch(string needle)
{
}

并得到相同的错误(也许是关于未使用参数的警告)。

是的,string是一种变量,但它不是C++语言本身与生俱来的,而是在标准库之一中,你必须告诉编译器:

#include "JohnSearch.h"
#include <string>
using std::string;
void sequentialSearch(string needle)
{
}

在包含的头文件中,您需要具有与 cpp 文件中的函数完全相同的签名。

另外不要忘记 #include ,然后使用像:std::string这样的字符串

例如

Int function(int number, int number2);

在你的 cpp 中

Int function(int number, int number2)
{
    // code
}

签名是"int function(int, int)"。