第一次在一个项目中使用 .h 和多个 .cpp 文件时,错误:(
first time using .h and multiple .cpp files in one project, errorssss :(
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
例如
Int function(int number, int number2);
在你的 cpp 中
Int function(int number, int number2)
{
// code
}
签名是"int function(int, int)"。
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 命名空间中具有.h和.cpp文件的类
- 无法编译 rtmidi 测试 cmidiin.cpp 文件, 非法指令
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- 有充分的理由在h文件中使用include保护而不是cpp文件吗
- 如何在cpp文件之间切换窗口?在Qt中
- 在 .h 文件中的类中声明静态变量和在.cpp文件中声明"global"变量有什么区别
- 我有两个类需要在同一 cpp 文件中相互引用,但第一个类无法识别第二个类类型的对象
- .h 和.cpp文件分离时出错,但仅使用 .h 文件时没有错误.我做错了什么?
- 库标题在标题中不可见,但在 cmake build 下.cpp文件中完全可见.为什么?
- C++ 链接到单独的.cpp文件说"multiple definitions"
- 在 cpp 文件中隐藏采用模板参数引用的方法
- 如何使用 samtools C API 构建一个简单的主.cpp文件
- 包含在.cpp文件中包含在 .h 文件时包含
- C++(.cpp文件和.h文件)拆分代码并添加一个函数,提取 - 这很容易吗?
- 在头文件和 cpp 文件中使用一次 #pragma 时出现结构重定义错误
- 如何使用线程类编译 cpp 文件?
- 使用 Powershell 命令将 cpp 文件的文件夹编译为 GNU 的 g++
- 我应该将外部标头放在 .h 文件还是.cpp文件中?
- 从另一个 cpp 文件更改结构内、映射键内的变量