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

Posted

技术标签:

【中文标题】第一次在一个项目中使用 .h 和多个 .cpp 文件,errorssss :(【英文标题】:first time using .h and multiple .cpp files in one project, errorssss :( 【发布时间】:2013-11-04 23:31:17 【问题描述】:

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

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

我的头文件

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

JohnSearch.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;

TestSearch.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 for JohnSearch.h 对吗? JohnSearch.h 中函数的前向声明是在 JohnSearch.cpp 中定义的,所以我需要这两个文件正确吗?

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

字符串仍未定义。

【问题讨论】:

编译器是怎么说的? std::string 也许?字符串类位于 std 命名空间中。 我们还没有学习类结构 :(@elimirks 编译器说“johnsearch.h(2): error C2065: 'string': undeclared identifier” 【参考方案1】:

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

您的头文件使用string,因此您需要在声明之前包含&lt;string&gt;。您还需要将其限定为 std::string 因为字符串类位于 std 命名空间中

所以你的头文件变成:

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

(你也应该在你的头文件中使用include guards)

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

在您的 TestSearch.cpp 中,您在顶部有一个 using namespace std;,您也可以在 JohnSearch.cpp 中执行相同的操作,这样您就可以使用 string 而不是 std::string

【讨论】:

【参考方案2】:

如有疑问,简化。您可以将代码归结为如下内容:

#include "JohnSearch.h"

void sequentialSearch(string needle)


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

是的,string 是一种变量,但它不是 C++ 语言本身固有的,它在一个标准库中,你必须告诉编译器:

#include "JohnSearch.h"
#include <string>
using std::string;

void sequentialSearch(string needle)


【讨论】:

我已经包含了 但它不起作用,我只是尝试使用 std::string;它消除了错误。这是为什么呢? @JtWeidenfeller:它与命名空间有关。如果您只包含库头文件,编译器不知道您希望它在那里查找string。如果你只是说using std::string;,编译器不知道任何这样的库。您成功使用using... 和没有#include... 的事实几乎可以肯定意味着JohnSearch.h 包含库头(可能间接地,通过包含包含它的东西)。 我完全改写了我的整个帖子,以便更好地描述实际发生的事情。我已经包含了到目前为止的所有代码和错误。即使使用#include string,我仍然会收到未定义字符串的错误。 :// 你声明sequentialSearch,它接受一个string参数(和一个string[]参数),你告诉编译器string是什么。将#include &lt;string&gt;using std::string; 放在标题的顶部,它至少会纠正一些错误。并且简化,这样你就可以一次解决一个问题。【参考方案3】:

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

也不要忘记#include ,然后使用类似的字符串:std::string

例如

Int function(int number, int number2);

在你的 cpp 中

Int function(int number, int number2)

    // code

签名是“int function(int, int)”。

【讨论】:

以上是关于第一次在一个项目中使用 .h 和多个 .cpp 文件,errorssss :(的主要内容,如果未能解决你的问题,请参考以下文章

使用gcc命令编译多个文件

请教 如何运行含多个cpp的一个c++项目里面的一个cpp

关于一个多个.cpp文件的项目中,函数出现未定义引用错误

使用makefile编译多个文件(.c , .cpp , .h等)

使用 G++ 编译多个 .cpp 和 .h 文件

使用多个类和链接列表