2013-11-04 60 views
-3

第二篇文章在StackOverflow上。我只是有一些一般性問題,爲什麼我的程序按照它的方式行事,我不想幫助完成它我星期五隻是缺課,顯然我錯過了很多 我的任務是設計一個包含3個.cpp和2個.h文件的程序,本質上它會搜索和排序字符串數組冒泡排序,插入排序,選擇排序方法以及順序搜索和二進制搜索,然後我們可以基於每種方法來確定哪一個是最快的搜索結果。沒有多少意義,我一直在這裏坐了大約一個小時擺弄不同的選擇離子或以不同方式鍵入代碼,但無濟於事。第一次在一個項目中使用.h和多個.cpp文件,錯誤:(

我的頭文件

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.h的JohnSearch.cpp對嗎? JohnSearch.h中函數的前向聲明是在JohnSearch.cpp中定義的,所以我需要這兩個文件是正確的?

我真的很困惑。我們應該修改的示例程序有2個.h文件和3個.cpp文件。其中2個.cpp文件對應於2個.h文件,所以我認爲我也需要2個.h文件和3個.cpp文件。

字符串仍未定義。

+0

編譯器說什麼? – elimirks

+0

'std :: string'也許?字符串類位於std名稱空間中。 –

+0

我們還沒有學過類結構:(@elimirks編譯器說:「johnsearch.h(2):錯誤C2065:'字符串':未聲明的標識符' –

回答

0

johnsearch.h(2):錯誤C2065: '串':未聲明的標識符

你的頭文件使用string,所以你需要包括<string>,您聲明之前。因爲string類駐留在std命名空間還需要限定它爲的std :: string

所以你的頭文件就變成了:

#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

0

如有疑問,簡化。您可以歸結代碼是這樣的:

#include "JohnSearch.h" 

void sequentialSearch(string needle) 
{ 
} 

,並得到了同樣的錯誤(也許我要講一個未使用的參數警告)。

是,string是一個類型的變量,但它不是在C先天++語言本身,它在標準庫的一個,你必須告訴編譯器的東西:

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

void sequentialSearch(string needle) 
{ 
} 
+0

我已經包含但它沒有工作,我只是嘗試使用std :: string;並且它刪除了錯誤爲什麼會這樣? –

+0

@JtWeidenfeller:它與* namespaces *有關。如果你只包含庫頭文件,編譯器不知道你想要它在那裏如果你只是說'使用std :: string;',編譯器不知道任何這樣的庫。事實上,你成功的使用了''而沒有'#include ...'幾乎肯定意味着'JohnSearch.h'包含了庫頭(可能間接地包含了包含它的東西) – Beta

+0

我完全重寫了我的整篇文章,以更好地描述實際發生的事情,包括了所有的代碼我迄今爲止還有錯誤與#include字符串ven我仍然得到錯誤,該字符串是未定義的。 :/ –

0

在你的頭包含的文件,您需要在cpp文件中具有與您的函數完全相同的簽名。

而且不要忘記#包括<串>,然後使用一個字符串,如:的std :: string

例如

Int function(int number, int number2); 

而在你的CPP

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

簽名是 「int函數(INT,INT)」。

相關問題